<!DOCTYPE html><html lang="zh-Hans"><head><meta charset="utf-8"><title>Angular - 风格指南</title><meta name="Description" content="Angular is a platform for building mobile and desktop web applications.
    Join the community of millions of developers who build compelling user interfaces with Angular."><base href="/"><meta name="viewport" content="width=device-width,initial-scale=1"><link rel="search" type="application/opensearchdescription+xml" href="assets/opensearch.xml"><link rel="icon" type="image/x-icon" href="assets/images/favicons/favicon.ico"><link rel="icon" type="image/png" href="assets/images/favicons/favicon-32x32.png" sizes="32x32"><link rel="icon" type="image/png" href="assets/images/favicons/favicon-194x194.png" sizes="194x194"><link rel="icon" type="image/png" href="assets/images/favicons/favicon-96x96.png" sizes="96x96"><link rel="icon" type="image/png" href="assets/images/favicons/favicon-16x16.png" sizes="16x16"><link rel="apple-touch-icon" sizes="144x144" href="assets/images/favicons/favicon-144x144.png"><link rel="apple-touch-icon-precomposed" sizes="144x144" href="assets/images/favicons/favicon-144x144.png"><link href="assets/fonts/Material_Icons.css" rel="stylesheet"><link href="assets/fonts/Droid_Sans_Mono.css" rel="stylesheet"><link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"><link rel="manifest" href="pwa-manifest.json"><meta name="theme-color" content="#1976d2"><meta name="apple-mobile-web-app-capable" content="yes"><meta name="apple-mobile-web-app-status-bar-style" content="translucent"><script async="" src="assets/js/analytics.js"></script><script>!function(e,a,n,t,s,c,g){e.GoogleAnalyticsObject=s,e.ga=e.ga||function(){(e.ga.q=e.ga.q||[]).push(arguments)},e.ga.l=1*new Date,c=a.createElement(n),g=a.getElementsByTagName(n)[0],c.async=1,c.src="assets/js/analytics.js",~e.name.indexOf("NG_DEFER_BOOTSTRAP")||g.parentNode.insertBefore(c,g)}(window,document,"script",0,"ga")</script><script>window.onerror=function(){ga("send","exception",{exDescription:function(e,r,n,a,c){var l;e=e.replace(/^Error: /,""),l=c?c.stack.replace(/^Error: /,"").replace(e+"\n","").replace(/^ +/gm,"").replace(/^at /gm,"").replace(/(?: \(|@)http.+\/([^/)]+)\)?(?:\n|$)/gm,"@$1\n").replace(/ *\(eval code(:\d+:\d+)\)(?:\n|$)/gm,"@???$1\n"):r+":"+(n=n||"?")+":"+(a=a||"?");return(e+"\n"+l).substr(0,150)}.apply(null,arguments),exFatal:!0})}</script><link rel="stylesheet" href="styles.4adbe52ad34df01b5273.css"><style>.cdk-high-contrast-active .mat-toolbar{outline:solid 1px}.mat-toolbar-row,.mat-toolbar-single-row{display:flex;box-sizing:border-box;padding:0 16px;width:100%;flex-direction:row;align-items:center;white-space:nowrap}.mat-toolbar-multiple-rows{display:flex;box-sizing:border-box;flex-direction:column;width:100%}.mat-toolbar-multiple-rows{min-height:64px}.mat-toolbar-row,.mat-toolbar-single-row{height:64px}@media(max-width:599px){.mat-toolbar-multiple-rows{min-height:56px}.mat-toolbar-row,.mat-toolbar-single-row{height:56px}}</style><style>.mat-icon{background-repeat:no-repeat;display:inline-block;fill:currentColor;height:24px;width:24px}.mat-icon.mat-icon-inline{font-size:inherit;height:inherit;line-height:inherit;width:inherit}[dir=rtl] .mat-icon-rtl-mirror{transform:scale(-1,1)}.mat-form-field:not(.mat-form-field-appearance-legacy) .mat-form-field-prefix .mat-icon,.mat-form-field:not(.mat-form-field-appearance-legacy) .mat-form-field-suffix .mat-icon{display:block}.mat-form-field:not(.mat-form-field-appearance-legacy) .mat-form-field-prefix .mat-icon-button .mat-icon,.mat-form-field:not(.mat-form-field-appearance-legacy) .mat-form-field-suffix .mat-icon-button .mat-icon{margin:auto}</style><style>.mat-button .mat-button-focus-overlay,.mat-icon-button .mat-button-focus-overlay{opacity:0}.mat-button:hover .mat-button-focus-overlay,.mat-stroked-button:hover .mat-button-focus-overlay{opacity:.04}@media(hover:none){.mat-button:hover .mat-button-focus-overlay,.mat-stroked-button:hover .mat-button-focus-overlay{opacity:0}}.mat-button,.mat-flat-button,.mat-icon-button,.mat-stroked-button{box-sizing:border-box;position:relative;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;cursor:pointer;outline:0;border:none;-webkit-tap-highlight-color:transparent;display:inline-block;white-space:nowrap;text-decoration:none;vertical-align:baseline;text-align:center;margin:0;min-width:64px;line-height:36px;padding:0 16px;border-radius:4px;overflow:visible}.mat-button::-moz-focus-inner,.mat-flat-button::-moz-focus-inner,.mat-icon-button::-moz-focus-inner,.mat-stroked-button::-moz-focus-inner{border:0}.mat-button[disabled],.mat-flat-button[disabled],.mat-icon-button[disabled],.mat-stroked-button[disabled]{cursor:default}.mat-button.cdk-keyboard-focused .mat-button-focus-overlay,.mat-button.cdk-program-focused .mat-button-focus-overlay,.mat-flat-button.cdk-keyboard-focused .mat-button-focus-overlay,.mat-flat-button.cdk-program-focused .mat-button-focus-overlay,.mat-icon-button.cdk-keyboard-focused .mat-button-focus-overlay,.mat-icon-button.cdk-program-focused .mat-button-focus-overlay,.mat-stroked-button.cdk-keyboard-focused .mat-button-focus-overlay,.mat-stroked-button.cdk-program-focused .mat-button-focus-overlay{opacity:.12}.mat-button::-moz-focus-inner,.mat-flat-button::-moz-focus-inner,.mat-icon-button::-moz-focus-inner,.mat-stroked-button::-moz-focus-inner{border:0}.mat-raised-button{box-sizing:border-box;position:relative;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;cursor:pointer;outline:0;border:none;-webkit-tap-highlight-color:transparent;display:inline-block;white-space:nowrap;text-decoration:none;vertical-align:baseline;text-align:center;margin:0;min-width:64px;line-height:36px;padding:0 16px;border-radius:4px;overflow:visible;transform:translate3d(0,0,0);transition:background .4s cubic-bezier(.25,.8,.25,1),box-shadow 280ms cubic-bezier(.4,0,.2,1)}.mat-raised-button::-moz-focus-inner{border:0}.mat-raised-button[disabled]{cursor:default}.mat-raised-button.cdk-keyboard-focused .mat-button-focus-overlay,.mat-raised-button.cdk-program-focused .mat-button-focus-overlay{opacity:.12}.mat-raised-button::-moz-focus-inner{border:0}._mat-animation-noopable.mat-raised-button{transition:none;animation:none}.mat-stroked-button{border:1px solid currentColor;padding:0 15px;line-height:34px}.mat-stroked-button .mat-button-focus-overlay,.mat-stroked-button .mat-button-ripple.mat-ripple{top:-1px;left:-1px;right:-1px;bottom:-1px}.mat-fab{box-sizing:border-box;position:relative;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;cursor:pointer;outline:0;border:none;-webkit-tap-highlight-color:transparent;display:inline-block;white-space:nowrap;text-decoration:none;vertical-align:baseline;text-align:center;margin:0;min-width:64px;line-height:36px;padding:0 16px;border-radius:4px;overflow:visible;transform:translate3d(0,0,0);transition:background .4s cubic-bezier(.25,.8,.25,1),box-shadow 280ms cubic-bezier(.4,0,.2,1);min-width:0;border-radius:50%;width:56px;height:56px;padding:0;flex-shrink:0}.mat-fab::-moz-focus-inner{border:0}.mat-fab[disabled]{cursor:default}.mat-fab.cdk-keyboard-focused .mat-button-focus-overlay,.mat-fab.cdk-program-focused .mat-button-focus-overlay{opacity:.12}.mat-fab::-moz-focus-inner{border:0}._mat-animation-noopable.mat-fab{transition:none;animation:none}.mat-fab .mat-button-wrapper{padding:16px 0;display:inline-block;line-height:24px}.mat-mini-fab{box-sizing:border-box;position:relative;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;cursor:pointer;outline:0;border:none;-webkit-tap-highlight-color:transparent;display:inline-block;white-space:nowrap;text-decoration:none;vertical-align:baseline;text-align:center;margin:0;min-width:64px;line-height:36px;padding:0 16px;border-radius:4px;overflow:visible;transform:translate3d(0,0,0);transition:background .4s cubic-bezier(.25,.8,.25,1),box-shadow 280ms cubic-bezier(.4,0,.2,1);min-width:0;border-radius:50%;width:40px;height:40px;padding:0;flex-shrink:0}.mat-mini-fab::-moz-focus-inner{border:0}.mat-mini-fab[disabled]{cursor:default}.mat-mini-fab.cdk-keyboard-focused .mat-button-focus-overlay,.mat-mini-fab.cdk-program-focused .mat-button-focus-overlay{opacity:.12}.mat-mini-fab::-moz-focus-inner{border:0}._mat-animation-noopable.mat-mini-fab{transition:none;animation:none}.mat-mini-fab .mat-button-wrapper{padding:8px 0;display:inline-block;line-height:24px}.mat-icon-button{padding:0;min-width:0;width:40px;height:40px;flex-shrink:0;line-height:40px;border-radius:50%}.mat-icon-button .mat-icon,.mat-icon-button i{line-height:24px}.mat-button-focus-overlay,.mat-button-ripple.mat-ripple{top:0;left:0;right:0;bottom:0;position:absolute;pointer-events:none;border-radius:inherit}.mat-button-ripple.mat-ripple:not(:empty){transform:translateZ(0)}.mat-button-focus-overlay{opacity:0;transition:opacity .2s cubic-bezier(.35,0,.25,1),background-color .2s cubic-bezier(.35,0,.25,1)}._mat-animation-noopable .mat-button-focus-overlay{transition:none}.cdk-high-contrast-active .mat-button-focus-overlay{background-color:#fff}.cdk-high-contrast-black-on-white .mat-button-focus-overlay{background-color:#000}.mat-button-ripple-round{border-radius:50%;z-index:1}.mat-button .mat-button-wrapper>*,.mat-fab .mat-button-wrapper>*,.mat-flat-button .mat-button-wrapper>*,.mat-icon-button .mat-button-wrapper>*,.mat-mini-fab .mat-button-wrapper>*,.mat-raised-button .mat-button-wrapper>*,.mat-stroked-button .mat-button-wrapper>*{vertical-align:middle}.mat-form-field:not(.mat-form-field-appearance-legacy) .mat-form-field-prefix .mat-icon-button,.mat-form-field:not(.mat-form-field-appearance-legacy) .mat-form-field-suffix .mat-icon-button{display:block;font-size:inherit;width:2.5em;height:2.5em}.cdk-high-contrast-active .mat-button,.cdk-high-contrast-active .mat-fab,.cdk-high-contrast-active .mat-flat-button,.cdk-high-contrast-active .mat-icon-button,.cdk-high-contrast-active .mat-mini-fab,.cdk-high-contrast-active .mat-raised-button{outline:solid 1px}</style><style>.mat-drawer-container{position:relative;z-index:1;box-sizing:border-box;-webkit-overflow-scrolling:touch;display:block;overflow:hidden}.mat-drawer-container[fullscreen]{top:0;left:0;right:0;bottom:0;position:absolute}.mat-drawer-container[fullscreen].mat-drawer-container-has-open{overflow:hidden}.mat-drawer-container.mat-drawer-container-explicit-backdrop .mat-drawer-side{z-index:3}.mat-drawer-container.ng-animate-disabled .mat-drawer-backdrop,.mat-drawer-container.ng-animate-disabled .mat-drawer-content,.ng-animate-disabled .mat-drawer-container .mat-drawer-backdrop,.ng-animate-disabled .mat-drawer-container .mat-drawer-content{transition:none}.mat-drawer-backdrop{top:0;left:0;right:0;bottom:0;position:absolute;display:block;z-index:3;visibility:hidden}.mat-drawer-backdrop.mat-drawer-shown{visibility:visible}.mat-drawer-transition .mat-drawer-backdrop{transition-duration:.4s;transition-timing-function:cubic-bezier(.25,.8,.25,1);transition-property:background-color,visibility}.cdk-high-contrast-active .mat-drawer-backdrop{opacity:.5}.mat-drawer-content{position:relative;z-index:1;display:block;height:100%;overflow:auto}.mat-drawer-transition .mat-drawer-content{transition-duration:.4s;transition-timing-function:cubic-bezier(.25,.8,.25,1);transition-property:transform,margin-left,margin-right}.mat-drawer{position:relative;z-index:4;display:block;position:absolute;top:0;bottom:0;z-index:3;outline:0;box-sizing:border-box;overflow-y:auto;transform:translate3d(-100%,0,0)}.cdk-high-contrast-active .mat-drawer,.cdk-high-contrast-active [dir=rtl] .mat-drawer.mat-drawer-end{border-right:solid 1px currentColor}.cdk-high-contrast-active .mat-drawer.mat-drawer-end,.cdk-high-contrast-active [dir=rtl] .mat-drawer{border-left:solid 1px currentColor;border-right:none}.mat-drawer.mat-drawer-side{z-index:2}.mat-drawer.mat-drawer-end{right:0;transform:translate3d(100%,0,0)}[dir=rtl] .mat-drawer{transform:translate3d(100%,0,0)}[dir=rtl] .mat-drawer.mat-drawer-end{left:0;right:auto;transform:translate3d(-100%,0,0)}.mat-drawer-inner-container{width:100%;height:100%;overflow:auto;-webkit-overflow-scrolling:touch}.mat-sidenav-fixed{position:fixed}</style><style>.nav-link.highlight[_ngcontent-ng-docs-c30]{color:#ff0}</style><style>.mat-progress-bar{display:block;height:4px;overflow:hidden;position:relative;transition:opacity 250ms linear;width:100%}._mat-animation-noopable.mat-progress-bar{transition:none;animation:none}.mat-progress-bar .mat-progress-bar-element,.mat-progress-bar .mat-progress-bar-fill::after{height:100%;position:absolute;width:100%}.mat-progress-bar .mat-progress-bar-background{width:calc(100% + 10px)}.cdk-high-contrast-active .mat-progress-bar .mat-progress-bar-background{display:none}.mat-progress-bar .mat-progress-bar-buffer{transform-origin:top left;transition:transform 250ms ease}.cdk-high-contrast-active .mat-progress-bar .mat-progress-bar-buffer{border-top:solid 5px;opacity:.5}.mat-progress-bar .mat-progress-bar-secondary{display:none}.mat-progress-bar .mat-progress-bar-fill{animation:none;transform-origin:top left;transition:transform 250ms ease}.cdk-high-contrast-active .mat-progress-bar .mat-progress-bar-fill{border-top:solid 4px}.mat-progress-bar .mat-progress-bar-fill::after{animation:none;content:"";display:inline-block;left:0}.mat-progress-bar[dir=rtl],[dir=rtl] .mat-progress-bar{transform:rotateY(180deg)}.mat-progress-bar[mode=query]{transform:rotateZ(180deg)}.mat-progress-bar[mode=query][dir=rtl],[dir=rtl] .mat-progress-bar[mode=query]{transform:rotateZ(180deg) rotateY(180deg)}.mat-progress-bar[mode=indeterminate] .mat-progress-bar-fill,.mat-progress-bar[mode=query] .mat-progress-bar-fill{transition:none}.mat-progress-bar[mode=indeterminate] .mat-progress-bar-primary,.mat-progress-bar[mode=query] .mat-progress-bar-primary{-webkit-backface-visibility:hidden;backface-visibility:hidden;animation:mat-progress-bar-primary-indeterminate-translate 2s infinite linear;left:-145.166611%}.mat-progress-bar[mode=indeterminate] .mat-progress-bar-primary.mat-progress-bar-fill::after,.mat-progress-bar[mode=query] .mat-progress-bar-primary.mat-progress-bar-fill::after{-webkit-backface-visibility:hidden;backface-visibility:hidden;animation:mat-progress-bar-primary-indeterminate-scale 2s infinite linear}.mat-progress-bar[mode=indeterminate] .mat-progress-bar-secondary,.mat-progress-bar[mode=query] .mat-progress-bar-secondary{-webkit-backface-visibility:hidden;backface-visibility:hidden;animation:mat-progress-bar-secondary-indeterminate-translate 2s infinite linear;left:-54.888891%;display:block}.mat-progress-bar[mode=indeterminate] .mat-progress-bar-secondary.mat-progress-bar-fill::after,.mat-progress-bar[mode=query] .mat-progress-bar-secondary.mat-progress-bar-fill::after{-webkit-backface-visibility:hidden;backface-visibility:hidden;animation:mat-progress-bar-secondary-indeterminate-scale 2s infinite linear}.mat-progress-bar[mode=buffer] .mat-progress-bar-background{-webkit-backface-visibility:hidden;backface-visibility:hidden;animation:mat-progress-bar-background-scroll 250ms infinite linear;display:block}.mat-progress-bar._mat-animation-noopable .mat-progress-bar-background,.mat-progress-bar._mat-animation-noopable .mat-progress-bar-buffer,.mat-progress-bar._mat-animation-noopable .mat-progress-bar-fill,.mat-progress-bar._mat-animation-noopable .mat-progress-bar-fill::after,.mat-progress-bar._mat-animation-noopable .mat-progress-bar-primary,.mat-progress-bar._mat-animation-noopable .mat-progress-bar-primary.mat-progress-bar-fill::after,.mat-progress-bar._mat-animation-noopable .mat-progress-bar-secondary,.mat-progress-bar._mat-animation-noopable .mat-progress-bar-secondary.mat-progress-bar-fill::after{animation:none;transition:none}@keyframes mat-progress-bar-primary-indeterminate-translate{0%{transform:translateX(0)}20%{animation-timing-function:cubic-bezier(.5,0,.701732,.495819);transform:translateX(0)}59.15%{animation-timing-function:cubic-bezier(.302435,.381352,.55,.956352);transform:translateX(83.67142%)}100%{transform:translateX(200.611057%)}}@keyframes mat-progress-bar-primary-indeterminate-scale{0%{transform:scaleX(.08)}36.65%{animation-timing-function:cubic-bezier(.334731,.12482,.785844,1);transform:scaleX(.08)}69.15%{animation-timing-function:cubic-bezier(.06,.11,.6,1);transform:scaleX(.661479)}100%{transform:scaleX(.08)}}@keyframes mat-progress-bar-secondary-indeterminate-translate{0%{animation-timing-function:cubic-bezier(.15,0,.515058,.409685);transform:translateX(0)}25%{animation-timing-function:cubic-bezier(.31033,.284058,.8,.733712);transform:translateX(37.651913%)}48.35%{animation-timing-function:cubic-bezier(.4,.627035,.6,.902026);transform:translateX(84.386165%)}100%{transform:translateX(160.277782%)}}@keyframes mat-progress-bar-secondary-indeterminate-scale{0%{animation-timing-function:cubic-bezier(.15,0,.515058,.409685);transform:scaleX(.08)}19.15%{animation-timing-function:cubic-bezier(.31033,.284058,.8,.733712);transform:scaleX(.457104)}44.15%{animation-timing-function:cubic-bezier(.4,.627035,.6,.902026);transform:scaleX(.72796)}100%{transform:scaleX(.08)}}@keyframes mat-progress-bar-background-scroll{to{transform:translateX(-8px)}}</style><script charset="utf-8" src="toc-toc-module-es2015.f8531f591ee147aebcc6.js"></script><script charset="utf-8" src="default~code-code-example-module~code-code-tabs-module-es2015.9efcc76d2979a2e605e0.js"></script><script charset="utf-8" src="code-code-example-module-es2015.fa05e6f700453813d256.js"></script><script charset="utf-8" src="code-code-tabs-module-es2015.505e1e0a869946918011.js"></script><script charset="utf-8" src="assets-js-prettify-js-es2015.ba856648f2dd2d930a9b.js"></script><style>.mat-card{transition:box-shadow 280ms cubic-bezier(.4,0,.2,1);display:block;position:relative;padding:16px;border-radius:4px}._mat-animation-noopable.mat-card{transition:none;animation:none}.mat-card .mat-divider-horizontal{position:absolute;left:0;width:100%}[dir=rtl] .mat-card .mat-divider-horizontal{left:auto;right:0}.mat-card .mat-divider-horizontal.mat-divider-inset{position:static;margin:0}[dir=rtl] .mat-card .mat-divider-horizontal.mat-divider-inset{margin-right:0}.cdk-high-contrast-active .mat-card{outline:solid 1px}.mat-card-actions,.mat-card-content,.mat-card-subtitle{display:block;margin-bottom:16px}.mat-card-title{display:block;margin-bottom:8px}.mat-card-actions{margin-left:-8px;margin-right:-8px;padding:8px 0}.mat-card-actions-align-end{display:flex;justify-content:flex-end}.mat-card-image{width:calc(100% + 32px);margin:0 -16px 16px -16px}.mat-card-footer{display:block;margin:0 -16px -16px -16px}.mat-card-actions .mat-button,.mat-card-actions .mat-raised-button,.mat-card-actions .mat-stroked-button{margin:0 8px}.mat-card-header{display:flex;flex-direction:row}.mat-card-header .mat-card-title{margin-bottom:12px}.mat-card-header-text{margin:0 16px}.mat-card-avatar{height:40px;width:40px;border-radius:50%;flex-shrink:0;object-fit:cover}.mat-card-title-group{display:flex;justify-content:space-between}.mat-card-sm-image{width:80px;height:80px}.mat-card-md-image{width:112px;height:112px}.mat-card-lg-image{width:152px;height:152px}.mat-card-xl-image{width:240px;height:240px;margin:-8px}.mat-card-title-group>.mat-card-xl-image{margin:-8px 0 8px}@media(max-width:599px){.mat-card-title-group{margin:0}.mat-card-xl-image{margin-left:0;margin-right:0}}.mat-card-content>:first-child,.mat-card>:first-child{margin-top:0}.mat-card-content>:last-child:not(.mat-card-footer),.mat-card>:last-child:not(.mat-card-footer){margin-bottom:0}.mat-card-image:first-child{margin-top:-16px;border-top-left-radius:inherit;border-top-right-radius:inherit}.mat-card>.mat-card-actions:last-child{margin-bottom:-8px;padding-bottom:0}.mat-card-actions .mat-button:first-child,.mat-card-actions .mat-raised-button:first-child,.mat-card-actions .mat-stroked-button:first-child{margin-left:0;margin-right:0}.mat-card-subtitle:not(:first-child),.mat-card-title:not(:first-child){margin-top:-4px}.mat-card-header .mat-card-subtitle:not(:first-child){margin-top:-8px}.mat-card>.mat-card-xl-image:first-child{margin-top:-8px}.mat-card>.mat-card-xl-image:last-child{margin-bottom:-8px}</style><style>.mat-tab-group{display:flex;flex-direction:column}.mat-tab-group.mat-tab-group-inverted-header{flex-direction:column-reverse}.mat-tab-label{height:48px;padding:0 24px;cursor:pointer;box-sizing:border-box;opacity:.6;min-width:160px;text-align:center;display:inline-flex;justify-content:center;align-items:center;white-space:nowrap;position:relative}.mat-tab-label:focus{outline:0}.mat-tab-label:focus:not(.mat-tab-disabled){opacity:1}.cdk-high-contrast-active .mat-tab-label:focus{outline:dotted 2px}.mat-tab-label.mat-tab-disabled{cursor:default}.cdk-high-contrast-active .mat-tab-label.mat-tab-disabled{opacity:.5}.mat-tab-label .mat-tab-label-content{display:inline-flex;justify-content:center;align-items:center;white-space:nowrap}.cdk-high-contrast-active .mat-tab-label{opacity:1}@media(max-width:599px){.mat-tab-label{padding:0 12px}}@media(max-width:959px){.mat-tab-label{padding:0 12px}}.mat-tab-group[mat-stretch-tabs]>.mat-tab-header .mat-tab-label{flex-basis:0;flex-grow:1}.mat-tab-body-wrapper{position:relative;overflow:hidden;display:flex;transition:height .5s cubic-bezier(.35,0,.25,1)}._mat-animation-noopable.mat-tab-body-wrapper{transition:none;animation:none}.mat-tab-body{top:0;left:0;right:0;bottom:0;position:absolute;display:block;overflow:hidden;flex-basis:100%}.mat-tab-body.mat-tab-body-active{position:relative;overflow-x:hidden;overflow-y:auto;z-index:1;flex-grow:1}.mat-tab-group.mat-tab-group-dynamic-height .mat-tab-body.mat-tab-body-active{overflow-y:hidden}</style><style>.mat-tab-header{display:flex;overflow:hidden;position:relative;flex-shrink:0}.mat-tab-header-pagination{-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;position:relative;display:none;justify-content:center;align-items:center;min-width:32px;cursor:pointer;z-index:2;-webkit-tap-highlight-color:transparent;touch-action:none}.mat-tab-header-pagination-controls-enabled .mat-tab-header-pagination{display:flex}.mat-tab-header-pagination-before,.mat-tab-header-rtl .mat-tab-header-pagination-after{padding-left:4px}.mat-tab-header-pagination-before .mat-tab-header-pagination-chevron,.mat-tab-header-rtl .mat-tab-header-pagination-after .mat-tab-header-pagination-chevron{transform:rotate(-135deg)}.mat-tab-header-pagination-after,.mat-tab-header-rtl .mat-tab-header-pagination-before{padding-right:4px}.mat-tab-header-pagination-after .mat-tab-header-pagination-chevron,.mat-tab-header-rtl .mat-tab-header-pagination-before .mat-tab-header-pagination-chevron{transform:rotate(45deg)}.mat-tab-header-pagination-chevron{border-style:solid;border-width:2px 2px 0 0;content:"";height:8px;width:8px}.mat-tab-header-pagination-disabled{box-shadow:none;cursor:default}.mat-tab-list{flex-grow:1;position:relative;transition:transform .5s cubic-bezier(.35,0,.25,1)}.mat-ink-bar{position:absolute;bottom:0;height:2px;transition:.5s cubic-bezier(.35,0,.25,1)}._mat-animation-noopable.mat-ink-bar{transition:none;animation:none}.mat-tab-group-inverted-header .mat-ink-bar{bottom:auto;top:0}.cdk-high-contrast-active .mat-ink-bar{outline:solid 2px;height:0}.mat-tab-labels{display:flex}[mat-align-tabs=center] .mat-tab-labels{justify-content:center}[mat-align-tabs=end] .mat-tab-labels{justify-content:flex-end}.mat-tab-label-container{display:flex;flex-grow:1;overflow:hidden;z-index:1}._mat-animation-noopable.mat-tab-list{transition:none;animation:none}.mat-tab-label{height:48px;padding:0 24px;cursor:pointer;box-sizing:border-box;opacity:.6;min-width:160px;text-align:center;display:inline-flex;justify-content:center;align-items:center;white-space:nowrap;position:relative}.mat-tab-label:focus{outline:0}.mat-tab-label:focus:not(.mat-tab-disabled){opacity:1}.cdk-high-contrast-active .mat-tab-label:focus{outline:dotted 2px}.mat-tab-label.mat-tab-disabled{cursor:default}.cdk-high-contrast-active .mat-tab-label.mat-tab-disabled{opacity:.5}.mat-tab-label .mat-tab-label-content{display:inline-flex;justify-content:center;align-items:center;white-space:nowrap}.cdk-high-contrast-active .mat-tab-label{opacity:1}@media(max-width:599px){.mat-tab-label{min-width:72px}}</style><style>.mat-tab-body-content{height:100%;overflow:auto}.mat-tab-group-dynamic-height .mat-tab-body-content{overflow:hidden}</style></head><body><aio-shell ng-version="9.0.0-rc.11" class="aio-notification-hide folder-guide mode-stable page-guide-styleguide sidenav-open view-SideNav"><div id="top-of-page"></div><mat-toolbar color="primary" class="mat-toolbar app-toolbar no-print mat-primary mat-toolbar-multiple-rows"><mat-toolbar-row class="mat-toolbar-row notification-container"><aio-notification notificationid="survey-february-2019" expirationdate="2019-03-01" class="ng-tns-c22-0 ng-trigger ng-trigger-hideAnimation" style="height:0"><span class="content ng-tns-c22-0"><a href="http://bit.ly/angular-survey-2019" target="_blank" class="ng-tns-c22-0"><mat-icon role="img" svgicon="insert_comment" aria-label="Announcement" class="mat-icon notranslate icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M20 2H4c-1.1 0-2 .9-2 2v12c0 1.1.9 2 2 2h14l4 4V4c0-1.1-.9-2-2-2zm-2 12H6v-2h12v2zm0-3H6V9h12v2zm0-3H6V6h12v2z"></path><path d="M0 0h24v24H0z" fill="none"></path></svg></mat-icon><span class="message"><b>填写这份《一分钟调查》</b>，帮我们（开发组）做得更好！</span><span class="action-button">去填写</span></a></span><button mat-icon-button="" aria-label="Close" class="close-button ng-tns-c22-0 mat-icon-button mat-button-base"><span class="mat-button-wrapper"><mat-icon role="img" svgicon="close" aria-label="Dismiss notification" class="mat-icon notranslate mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z"></path><path d="M0 0h24v24H0z" fill="none"></path></svg></mat-icon></span><div matripple="" class="mat-ripple mat-button-ripple mat-button-ripple-round"></div><div class="mat-button-focus-overlay"></div></button></aio-notification></mat-toolbar-row><mat-toolbar-row class="mat-toolbar-row"><button mat-button="" title="Docs menu" class="hamburger mat-button mat-button-base"><span class="mat-button-wrapper"><mat-icon role="img" svgicon="menu" class="mat-icon notranslate mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M3 18h18v-2H3v2zm0-5h18v-2H3v2zm0-7v2h18V6H3z"></path></svg></mat-icon></span><div matripple="" class="mat-ripple mat-button-ripple"></div><div class="mat-button-focus-overlay"></div></button><a href="/" class="nav-link home"><img src="assets/images/logos/angular/logo-nav@2x.png" width="150" height="40" title="Home" alt="Home" class="ng-star-inserted"></a><aio-top-menu _nghost-ng-docs-c30="" class="ng-star-inserted"><ul _ngcontent-ng-docs-c30="" role="navigation"><li _ngcontent-ng-docs-c30="" class="ng-star-inserted"><a _ngcontent-ng-docs-c30="" class="nav-link" href="features" title="特性"><span _ngcontent-ng-docs-c30="" class="nav-link-inner">特性</span></a></li><li _ngcontent-ng-docs-c30="" class="ng-star-inserted"><a _ngcontent-ng-docs-c30="" class="nav-link" href="docs" title="文档"><span _ngcontent-ng-docs-c30="" class="nav-link-inner">文档</span></a></li><li _ngcontent-ng-docs-c30="" class="ng-star-inserted"><a _ngcontent-ng-docs-c30="" class="nav-link" href="resources" title="资源"><span _ngcontent-ng-docs-c30="" class="nav-link-inner">资源</span></a></li><li _ngcontent-ng-docs-c30="" class="ng-star-inserted"><a _ngcontent-ng-docs-c30="" class="nav-link" href="events" title="会议"><span _ngcontent-ng-docs-c30="" class="nav-link-inner">会议</span></a></li><li _ngcontent-ng-docs-c30="" class="ng-star-inserted"><a _ngcontent-ng-docs-c30="" class="nav-link" href="https://blog.angular.io/" title="博客"><span _ngcontent-ng-docs-c30="" class="nav-link-inner">博客</span></a></li><li _ngcontent-ng-docs-c30="" class="ng-star-inserted"><a _ngcontent-ng-docs-c30="" class="nav-link" href="translations/cn/home" title="关于中文版"><span _ngcontent-ng-docs-c30="" class="nav-link-inner">关于中文版</span></a></li></ul></aio-top-menu><aio-search-box class="search-container"><input type="search" aria-label="search" placeholder="搜索"></aio-search-box><div class="toolbar-external-icons-container"><a href="https://twitter.com/angular" title="Twitter" aria-label="Angular on twitter"><mat-icon role="img" svgicon="logos:twitter" class="mat-icon notranslate mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 50 59" xmlns="http://www.w3.org/2000/svg"><path d="M50,9.3c-1.8,0.8-3.8,1.4-5.9,1.6c2.1-1.3,3.7-3.3,4.5-5.7c-2,1.2-4.2,2-6.5,2.5c-1.9-2-4.5-3.2-7.5-3.2c-5.7,0-10.3,4.6-10.3,10.3c0,0.8,0.1,1.6,0.3,2.3C16.1,16.7,8.5,12.6,3.5,6.4c-0.9,1.5-1.4,3.3-1.4,5.2c0,3.6,1.8,6.7,4.6,8.5C5,20,3.4,19.6,2,18.8c0,0,0,0.1,0,0.1c0,5,3.5,9.1,8.2,10.1c-0.9,0.2-1.8,0.4-2.7,0.4c-0.7,0-1.3-0.1-1.9-0.2c1.3,4.1,5.1,7,9.6,7.1c-3.5,2.8-7.9,4.4-12.7,4.4c-0.8,0-1.6,0-2.4-0.1c4.5,2.9,9.9,4.6,15.7,4.6c18.9,0,29.2-15.6,29.2-29.2c0-0.4,0-0.9,0-1.3C46.9,13.2,48.6,11.4,50,9.3z"></path></svg></mat-icon></a><a href="https://github.com/angular/angular" title="GitHub" aria-label="Angular on github"><mat-icon role="img" svgicon="logos:github" class="mat-icon notranslate mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 51.8 50.4" xmlns="http://www.w3.org/2000/svg"><path d="M25.9,0.2C11.8,0.2,0.3,11.7,0.3,25.8c0,11.3,7.3,20.9,17.5,24.3c1.3,0.2,1.7-0.6,1.7-1.2c0-0.6,0-2.6,0-4.8c-7.1,1.5-8.6-3-8.6-3c-1.2-3-2.8-3.7-2.8-3.7c-2.3-1.6,0.2-1.6,0.2-1.6c2.6,0.2,3.9,2.6,3.9,2.6c2.3,3.9,6,2.8,7.5,2.1c0.2-1.7,0.9-2.8,1.6-3.4c-5.7-0.6-11.7-2.8-11.7-12.7c0-2.8,1-5.1,2.6-6.9c-0.3-0.7-1.1-3.3,0.3-6.8c0,0,2.1-0.7,7,2.6c2-0.6,4.2-0.9,6.4-0.9c2.2,0,4.4,0.3,6.4,0.9c4.9-3.3,7-2.6,7-2.6c1.4,3.5,0.5,6.1,0.3,6.8c1.6,1.8,2.6,4.1,2.6,6.9c0,9.8-6,12-11.7,12.6c0.9,0.8,1.7,2.4,1.7,4.7c0,3.4,0,6.2,0,7c0,0.7,0.5,1.5,1.8,1.2c10.2-3.4,17.5-13,17.5-24.3C51.5,11.7,40.1,0.2,25.9,0.2z"></path></svg></mat-icon></a></div></mat-toolbar-row></mat-toolbar><mat-sidenav-container role="main" class="mat-drawer-container mat-sidenav-container sidenav-container mat-drawer-transition has-floating-toc"><div class="mat-drawer-backdrop ng-star-inserted"></div><div class="cdk-visually-hidden cdk-focus-trap-anchor" aria-hidden="true"></div><mat-sidenav tabindex="-1" class="mat-drawer mat-sidenav sidenav ng-tns-c18-1 ng-trigger ng-trigger-transform mat-drawer-side ng-star-inserted mat-drawer-opened" style="transform:none;visibility:visible"><div class="mat-drawer-inner-container ng-tns-c18-1"><aio-nav-menu class="ng-tns-c18-1"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-1 collapsed ng-star-inserted" style="position:relative" href="docs" title="Angular 文档简介" target="_self"><span>简介</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-1 collapsed ng-star-inserted" title="通过构建第一个 Angular 应用来学习基础知识" aria-pressed="false"><span>快速上手</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-1 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="start" title="介绍 Angular 的组件模型、模板语法和组件通讯" target="_self"><span>你的第一个应用</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="start/routing" title="介绍如何使用浏览器的 URL 在组件之间进行路由" target="_self"><span>路由</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="start/data" title="介绍服务以及如何访问外部数据" target="_self"><span>管理数据</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="start/forms" title="学习如何使用表单从用户那里获取并管理数据" target="_self"><span>表单</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="start/deployment" title="通过你的应用放到 Firebase 或自己的服务器上来把它分享给外界" target="_self"><span>部署</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-1 collapsed ng-star-inserted" style="position:relative" href="guide/setup-local" title="使用 Angular CLI 搭建本地开发环境简介" target="_self"><span>搭建环境</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-1 collapsed ng-star-inserted" title="Angular 的基本原理" aria-pressed="false"><span>基本原理</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-1 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-2 collapsed ng-star-inserted" title="介绍 Angular 应用中的一些基本概念。" aria-pressed="false"><span>Angular 的基本概念</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-2 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/architecture" title="Angular 应用中的基本构造块。" target="_self"><span>基本概念简介</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/architecture-modules" title="关于 Angular 模块（NgModules）。" target="_self"><span>模块简介</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/architecture-components" title="关于组件、模板和视图。" target="_self"><span>组件简介</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/architecture-services" title="关于服务与依赖注入。" target="_self"><span>服务与 DI 简介</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/architecture-next-steps" title="超越基础阶段。" target="_self"><span>后续步骤</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-2 collapsed ng-star-inserted" title="英雄指南是这里大量 Angular 范例的基础" aria-pressed="false"><span>英雄指南</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-2 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="tutorial" title="《英雄指南》教程简介" target="_self"><span>简介</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="tutorial/toh-pt0" title="创建应用的外壳" target="_self"><span>应用的“外壳”</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="tutorial/toh-pt1" title="第一部分：构建一个简单的英雄编辑器" target="_self"><span>1. 英雄编辑器</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="tutorial/toh-pt2" title="第二部分：构建一个主从结构的页面，用于展现英雄列表。" target="_self"><span>2. 显示英雄列表</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="tutorial/toh-pt3" title="第三部分：把主从结构的视图重构成几个独立的组件。" target="_self"><span>3. 创建特性组件</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="tutorial/toh-pt4" title="第四部分：创建一个可复用的服务来管理英雄数据。" target="_self"><span>4. 添加服务</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="tutorial/toh-pt5" title="第五部分：添加 Angular 路由器，并且学习在视图之间导航。" target="_self"><span>5. 添加应用内导航</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="tutorial/toh-pt6" title="第六部分：通过 HTTP 来获取并保存英雄数据。" target="_self"><span>6. 从服务器获取数据</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-2 collapsed ng-star-inserted" title="通过数据绑定构建动态视图" aria-pressed="false"><span>组件与模板</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-2 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/displaying-data" title="属性绑定可以帮助应用把数据显示在界面上" target="_self"><span>显示数据</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/template-syntax" title="学习如何写模板，以便借助数据绑定机制显示数据并响应事件。" target="_self"><span>模板语法</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/user-input" title="用户输入会触发 DOM 事件。Angular 会通过事件绑定来监听那些事件，并把修改后的值传回应用的组件和模型中。" target="_self"><span>用户输入</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/lifecycle-hooks" title="Angular 调用指令和组件的生命周期钩子函数，包括它的创建、变更和销毁时。" target="_self"><span>生命周期钩子</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/component-interaction" title="在不同的指令和组件之间共享信息" target="_self"><span>组件交互</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/component-styles" title="添加专属于某个组件的样式" target="_self"><span>组件样式</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/dynamic-component-loader" title="动态加载组件" target="_self"><span>动态组件</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/elements" title="把组件转换成自定义元素" target="_self"><span>Angular 元素</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/attribute-directives" title="属性型指令把行为添加到现有元素上。" target="_self"><span>属性型指令</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/structural-directives" title="结构型指令可以操纵页面的布局" target="_self"><span>结构型指令</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/pipes" title="管道可以在模板中转换显示的内容。" target="_self"><span>管道</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-2 collapsed ng-star-inserted" title="Angular 的表单" aria-pressed="false"><span>表单</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-2 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/forms-overview" title="表单可以创建集中、高效、引人注目的输入体验。Angular 表单可以协调一组数据绑定控件，跟踪变更，验证输入，并表达错误信息。" target="_self"><span>简介</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/reactive-forms" title="使用 FormBuilder、表单组和表单数组创建响应式表单。" target="_self"><span>响应式表单</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/forms" title="使用指令和 Angular 模板语法创建模板驱动表单。" target="_self"><span>模板驱动表单</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/form-validation" title="验证用户的表单输入" target="_self"><span>表单验证</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/dynamic-form" title="使用 FormGroup 渲染动态表单。" target="_self"><span>动态表单</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-2 collapsed ng-star-inserted" title="Observable 与 RxJS" aria-pressed="false"><span>Observable 与 RxJS</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-2 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/observables" title="" target="_self"><span>可观察对象(Observable)</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/rx-library" title="" target="_self"><span>RxJS 库</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/observables-in-angular" title="" target="_self"><span>Angular 中的可观察对象</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/practical-observable-usage" title="" target="_self"><span>用法实战</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/comparing-observables" title="" target="_self"><span>与其它技术的比较</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-2 collapsed ng-star-inserted" title="Angular 中的模块" aria-pressed="false"><span>NgModules</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-2 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/ngmodules" title="使用 NgModule 让你的应用更高效" target="_self"><span>NgModule 简介</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/ngmodule-vs-jsmodule" title="JavaScript 模块和 NgModule 之间的差异" target="_self"><span>JS 模块 vs NgModule</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/bootstrapping" title="告诉 Angular 如何在根 &quot;AppModule&quot; 中构造和引导应用。" target="_self"><span>应用的根模块</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/frequent-ngmodules" title="介绍最常用的 NgModule" target="_self"><span>常用模块</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/module-types" title="介绍特性模块的几种类型" target="_self"><span>特性模块的分类</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/entry-components" title="关于 Angular 中入口组件的一切" target="_self"><span>入口组件</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/feature-modules" title="创建特性模块，以组织你的代码" target="_self"><span>特性模块</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/providers" title="服务提供商与 NgModule" target="_self"><span>服务提供商</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/singleton-services" title="创建单例服务" target="_self"><span>单例服务</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/lazy-loading-ngmodules" title="惰性加载模块，以提高应用的性能" target="_self"><span>惰性加载的特性模块</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/sharing-ngmodules" title="共享 NgModule 让你的应用现代化。" target="_self"><span>共享 NgModule</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/ngmodule-api" title="理解 NgModule 的那些细节。" target="_self"><span>NgModule API</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/ngmodule-faq" title="回答关于 NgModules 的常见问题。" target="_self"><span>NgModule 常见问题</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-2 collapsed ng-star-inserted" title="依赖注入：创建并注入各种服务。" aria-pressed="false"><span>依赖注入</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-2 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/dependency-injection" title="Angular 的依赖注入系统能够为 Angular 创建的类创建并交付它们所依赖的服务。" target="_self"><span>Angular 依赖注入</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/hierarchical-dependency-injection" title="与组件树平行的注入器树，并支持嵌套的依赖。" target="_self"><span>多级注入器</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/dependency-injection-providers" title="各种提供商类型的更多知识。" target="_self"><span>DI 提供商</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/dependency-injection-in-action" title="依赖注入的使用技巧" target="_self"><span>DI 实战</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/dependency-injection-navtree" title="使用注入器树来查找父组件。" target="_self"><span>浏览组件树</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/http" title="通过 HTTP 协议与远程服务器对话。" target="_self"><span>HttpClient</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/router" title="揭示如何通过 Angular 路由进行基本的屏幕导航。" target="_self"><span>路由与导航</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-2 collapsed ng-star-inserted" title="Angular 动画系统指南" aria-pressed="false"><span>动画</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-2 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/animations" title="Angular 动画的基础技术。" target="_self"><span>简介</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/transition-and-triggers" title="转场与触发器的高级技术。" target="_self"><span>转场与触发器</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/complex-animation-sequences" title="复杂的 Angular 动画序列。" target="_self"><span>复杂序列</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/reusable-animations" title="创建可复用的动画。" target="_self"><span>可复用动画</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/route-animations" title="为路由提供转场动画。" target="_self"><span>路由转场动画</span></a></div></aio-nav-item></div></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-1 collapsed ng-star-inserted" title="把 Angular 用到你的实际工作中的一些技巧" aria-pressed="false"><span>其它技术</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-1 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/security" title="Angular 应用开发中的安全技术。" target="_self"><span>安全</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/i18n" title="把应用模板中的文本翻译成多种语言。" target="_self"><span>国际化（i18n）</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/accessibility" title="设计能被所有用户访问的应用" target="_self"><span>无障碍（a11y）</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-2 collapsed ng-star-inserted" title="Angular Service Worker: 控制应用资源的缓存。" aria-pressed="false"><span>Service Worker 与 PWA</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-2 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/service-worker-intro" title="Angular 对 Service Worker 的实现提升了慢速或不稳定的网络连接下的用户体验。" target="_self"><span>简介</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/service-worker-getting-started" title="在 CLI 项目中启用 Service Worker，并在浏览器中查看效果。" target="_self"><span>快速上手</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/app-shell" title="在 CLI 项目中启用应用外壳。" target="_self"><span>应用外壳</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/service-worker-communications" title="那些能让你和 Angular 的 Service Worker 通讯的服务类。" target="_self"><span>与 Service Worker 通讯</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/service-worker-devops" title="使用 Service Worker 运行应用、管理应用更新、调试以及杀掉正在运行的应用。" target="_self"><span>生产环境下的 Service Worker</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/service-worker-config" title="配置 Service Worker 的缓存行为。" target="_self"><span>Service Worker 配置</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/universal" title="使用 Angular Universal 在服务端渲染 HTML。" target="_self"><span>服务端渲染</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-2 collapsed ng-star-inserted" title="把 AngularJS 应用增量式的升级到 Angular。" aria-pressed="false"><span>从 AngularJS 升级</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-2 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/upgrade" title="把 AngularJS 应用增量式的升级到 Angular。" target="_self"><span>升级步骤</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/upgrade-performance" title="用更灵活的方式把 AngularJS 升级到 Angular。" target="_self"><span>更关注性能的升级方式</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/ajs-quick-reference" title="学习如何把 AngularJS 的概念映射到 Angular 中。" target="_self"><span>AngularJS 与 Angular 的概念对照</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-2 collapsed ng-star-inserted" title="使用共享库扩展 Angular" aria-pressed="false"><span>开发 Angular 库</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-2 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/libraries" title="理解何时以及如何使用和创建库。" target="_self"><span>库概览</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/using-libraries" title="把已发布的库集成进你的应用中。" target="_self"><span>使用已发布的库</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/creating-libraries" title="通过创建、发布和使用你自己的库来扩展 Angular。" target="_self"><span>创建库</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-2 collapsed ng-star-inserted" title="理解原理图" aria-pressed="false"><span>原理图（Schematic）</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-2 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/schematics" title="理解 Angular 如何使用原理图。" target="_self"><span>原理图概览</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/schematics-authoring" title="理解原理图的结构。" target="_self"><span>制作原理图</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/schematics-for-libraries" title="使用原理图来把你的库集成进 Angular CLI 中。" target="_self"><span>库的原理图</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/cli-builder" title="使用构建器定制 Angular CLI。" target="_self"><span>CLI 构建器</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/web-worker" title="在 Angular CLI 中使用 Web Worker。" target="_self"><span>Web Worker</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-1 collapsed ng-star-inserted" title="关于构建、测试和部署的信息" aria-pressed="false"><span>开发工作流</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-1 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-2 collapsed ng-star-inserted" title="理解 AOT 预先编译器。" aria-pressed="false"><span>AOT 预先编译器</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-2 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/aot-compiler" title="学习为何以及如何使用预先编译器。" target="_self"><span>预先编译</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/angular-compiler-options" title="配置 AOT 编译。" target="_self"><span>Angular 编译器选项</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/aot-metadata-errors" title="解决 AOT 编译错误。" target="_self"><span>AOT 元数据错误</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/template-typecheck" title="Angular 中的模板类型检查。" target="_self"><span>模板类型检查</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/build" title="构建应用及为应用启动开发服务器。" target="_self"><span>构建与运行</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/bazel" title="如何配置你的环境，以利用 Bazel 进行构建和测试。" target="_self"><span>使用 Bazel 进行构建</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/testing" title="测试 Angular 应用的技巧与实践。" target="_self"><span>测试</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/deployment" title="了解如何部署 Angular 应用。" target="_self"><span>发布</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-2 collapsed ng-star-inserted" title="与开发环境和工具集成起来" aria-pressed="false"><span>开发工具集成</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-2 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/language-service" title="使用 Angular 语言服务加速开发。" target="_self"><span>语言服务</span></a></div></aio-nav-item></div></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-1 collapsed ng-star-inserted" title="工作空间与项目的结构，及其配置文件。" aria-pressed="false"><span>配置</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-1 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/file-structure" title="Angular 工作区在文件系统中是怎样的。" target="_self"><span>项目文件结构</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/workspace-config" title="&quot;angular.json&quot; 包含供 CLI 命令使用的工作区和项目默认配置。" target="_self"><span>工作区配置</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/npm-packages" title="开发期间和运行期间所需的 npm 包的说明。" target="_self"><span>npm 包</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/typescript-configuration" title="给 Angular 开发者的 TypeScript 配置。" target="_self"><span>TypeScript 配置</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/browser-support" title="浏览器支持与腻子脚本指南。" target="_self"><span>浏览器支持</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-1 collapsed ng-star-inserted" title="Angular 的版本发布实践、更新与升级。" aria-pressed="false"><span>发布信息</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-1 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/updating" title="如何把 Angular 应用和库升级到最新版本。" target="_self"><span>保持最新</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/releases" title="Angular 的版本、发布、支持、弃用策略及实践。" target="_self"><span>发布实践</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/updating-to-version-9" title="支持把你的应用从 8 升级到 9。" target="_self"><span>升级到第 9 版</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/deprecations" title="弃用的 Angular API 和特性汇总。" target="_self"><span>弃用清单</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/ivy" title="关于 Angular Ivy 的编译与渲染管道。" target="_self"><span>Angular Ivy</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-1 expanded selected ng-star-inserted" title="Angular 语法、编码、术语汇总。" aria-pressed="true"><span>快捷手册</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-1 expanded selected"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/cheatsheet" title="关于 Angular 常用编码技术的快速指南。" target="_self"><span>速查表</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 expanded selected ng-star-inserted" style="position:relative" href="guide/styleguide" title="写出 Angular 风格的程序" target="_self"><span>风格指南</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/glossary" title="Angular 中最重要的词汇的简要定义。" target="_self"><span>词汇表</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-1 collapsed ng-star-inserted" title="Angular CLI 命令参考手册。" aria-pressed="false"><span>CLI 命令</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-1 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli" title="CLI 工具介绍、命令、语法" target="_self"><span>概览</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/usage-analytics-gathering" title="管理员如何从用户那里收集使用情况分析。" target="_self"><span>使用情况分析</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/add" title="ng add." target="_self"><span>ng add</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/analytics" title="ng analytics." target="_self"><span>ng analytics</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/build" title="ng build." target="_self"><span>ng build</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/config" title="ng config." target="_self"><span>ng config</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/deploy" title="ng deploy." target="_self"><span>ng deploy</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/doc" title="ng doc." target="_self"><span>ng doc</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/e2e" title="ng e2e." target="_self"><span>ng e2e</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/generate" title="ng generate." target="_self"><span>ng generate</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/help" title="ng help." target="_self"><span>ng help</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/lint" title="ng lint." target="_self"><span>ng lint</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/new" title="ng new." target="_self"><span>ng new</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/run" title="ng run." target="_self"><span>ng run</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/serve" title="ng serve." target="_self"><span>ng serve</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/test" title="ng test." target="_self"><span>ng test</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/update" title="ng update." target="_self"><span>ng update</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/version" title="ng version." target="_self"><span>ng version</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/xi18n" title="ng xi18n." target="_self"><span>ng xi18n</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-1 collapsed ng-star-inserted" style="position:relative" href="api" title="关于 Angular 中类和值的详细信息。" target="_self"><span>API 参考手册</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><div class="mat-divider ng-star-inserted" style="margin:4px 20px;border-top:1px solid #d3d3d3"></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-1 collapsed ng-star-inserted" style="position:relative" href="https://github.com/ng-docs/ng-docs.github.io/issues" title="github 上的中文互助问答区" target="_blank"><span>互助问答</span><mat-icon role="img" class="mat-icon notranslate material-icons mat-icon-no-color ng-star-inserted" aria-hidden="true">open_in_new</mat-icon></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-1 collapsed ng-star-inserted" style="position:relative" href="https://material.angular.cn" title="Angular Material 组件库的中文文档" target="_blank"><span>官方 Material 组件库</span><mat-icon role="img" class="mat-icon notranslate material-icons mat-icon-no-color ng-star-inserted" aria-hidden="true">open_in_new</mat-icon></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-1 collapsed ng-star-inserted" style="position:relative" href="https://ng.ant.design/" title="Ant Design 的 Angular 实现，服务于企业级后台产品。" target="_blank"><span>ng-zorro 组件库</span><mat-icon role="img" class="mat-icon notranslate material-icons mat-icon-no-color ng-star-inserted" aria-hidden="true">open_in_new</mat-icon></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-1 collapsed ng-star-inserted" style="position:relative" href="https://ng.mobile.ant.design/" title="Ant Design Mobile 的 Angular 实现，服务于无线产品。" target="_blank"><span>ng-zorro mobile 组件库</span><mat-icon role="img" class="mat-icon notranslate material-icons mat-icon-no-color ng-star-inserted" aria-hidden="true">open_in_new</mat-icon></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-1 collapsed ng-star-inserted" style="position:relative" href="https://flutter-io.cn/" title="Flutter 中文文档站" target="_blank"><span>友站：Flutter 中文</span><mat-icon role="img" class="mat-icon notranslate material-icons mat-icon-no-color ng-star-inserted" aria-hidden="true">open_in_new</mat-icon></a></div></aio-nav-item></aio-nav-menu><div class="doc-version ng-tns-c18-1"><aio-select><div class="form-select-menu"><button class="form-select-button"><span><strong></strong></span><span>stable (v9.0.0)</span></button></div></aio-select></div></div></mat-sidenav><div class="cdk-visually-hidden cdk-focus-trap-anchor" aria-hidden="true"></div><mat-sidenav-content cdkscrollable="" class="mat-drawer-content mat-sidenav-content ng-star-inserted" style="margin-left:261px"><main role="main" class="sidenav-content" id="guide-styleguide"><aio-mode-banner></aio-mode-banner><aio-doc-viewer class=""><div style="opacity:1"><div class="github-links"><a href="https://github.com/angular/angular-cn/edit/aio/aio/content/guide/styleguide.md?message=docs%3A%20请简述你的修改..." aria-label="提供编辑建议" title="提供编辑建议"><i class="material-icons" aria-hidden="true" role="img">mode_edit</i></a></div><div class="content"><h1 id="angular-coding-style-guide" translation-result="on">风格指南<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#angular-coding-style-guide"><i class="material-icons">link</i></a></h1><aio-toc class="embedded" ng-version="9.0.0-rc.11"><div class="toc-inner no-print collapsed ng-star-inserted"><button type="button" title="Expand/collapse contents" aria-label="Expand/collapse contents" class="toc-heading embedded secondary ng-star-inserted" aria-pressed="false">目录<mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon collapsed mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><ul class="toc-list embedded"><li class="h2 ng-star-inserted" title="风格指南的用词"><a href="guide/styleguide#style-vocabulary">风格指南的用词</a></li><li class="h2 ng-star-inserted" title="文件结构约定"><a href="guide/styleguide#file-structure-conventions">文件结构约定</a></li><li class="h2 ng-star-inserted" title="单一职责"><a href="guide/styleguide#single-responsibility">单一职责</a></li><li class="h3 secondary ng-star-inserted" title="单一规则"><a href="guide/styleguide#rule-of-one">单一规则</a></li><li class="h3 secondary ng-star-inserted" title="小函数"><a href="guide/styleguide#small-functions">小函数</a></li><li class="h2 secondary ng-star-inserted" title="命名"><a href="guide/styleguide#naming">命名</a></li><li class="h3 secondary ng-star-inserted" title="总体命名原则"><a href="guide/styleguide#general-naming-guidelines">总体命名原则</a></li><li class="h3 secondary ng-star-inserted" title="使用点和横杠来分隔文件名"><a href="guide/styleguide#separate-file-names-with-dots-and-dashes">使用点和横杠来分隔文件名</a></li><li class="h3 secondary ng-star-inserted" title="符号名与文件名"><a href="guide/styleguide#symbols-and-file-names">符号名与文件名</a></li><li class="h3 secondary ng-star-inserted" title="服务名"><a href="guide/styleguide#service-names">服务名</a></li><li class="h3 secondary ng-star-inserted" title="引导"><a href="guide/styleguide#bootstrapping">引导</a></li><li class="h3 secondary ng-star-inserted" title="组件选择器"><a href="guide/styleguide#component-selectors">组件选择器</a></li><li class="h3 secondary ng-star-inserted" title="为组件添加自定义前缀"><a href="guide/styleguide#component-custom-prefix">为组件添加自定义前缀</a></li><li class="h3 secondary ng-star-inserted" title="指令选择器"><a href="guide/styleguide#directive-selectors">指令选择器</a></li><li class="h3 secondary ng-star-inserted" title="为指令添加自定义前缀"><a href="guide/styleguide#directive-custom-prefix">为指令添加自定义前缀</a></li><li class="h3 secondary ng-star-inserted" title="管道名"><a href="guide/styleguide#pipe-names">管道名</a></li><li class="h3 secondary ng-star-inserted" title="单元测试文件名"><a href="guide/styleguide#unit-test-file-names">单元测试文件名</a></li><li class="h3 secondary ng-star-inserted" title="端到端（E2E）测试的文件名"><a href="guide/styleguide#end-to-end-e2e-test-file-names"><em>端到端</em>（E2E）测试的文件名</a></li><li class="h3 secondary ng-star-inserted" title="Angular NgModule 命名"><a href="guide/styleguide#angular-ngmodule-names">Angular <em>NgModule</em> 命名</a></li><li class="h2 secondary ng-star-inserted" title="应用程序结构与 NgModule"><a href="guide/styleguide#application-structure-and-ngmodules">应用程序结构与 NgModule</a></li><li class="h3 secondary ng-star-inserted" title="LIFT"><a href="guide/styleguide#lift"><em>LIFT</em></a></li><li class="h3 secondary ng-star-inserted" title="定位"><a href="guide/styleguide#locate">定位</a></li><li class="h3 secondary ng-star-inserted" title="识别"><a href="guide/styleguide#identify">识别</a></li><li class="h3 secondary ng-star-inserted" title="扁平"><a href="guide/styleguide#flat">扁平</a></li><li class="h3 secondary ng-star-inserted" title="T-DRY（尽量不重复自己）"><a href="guide/styleguide#t-dry-try-to-be-dry"><em>T-DRY</em>（尽量不重复自己）</a></li><li class="h3 secondary ng-star-inserted" title="总体结构的指导原则"><a href="guide/styleguide#overall-structural-guidelines">总体结构的指导原则</a></li><li class="h3 secondary ng-star-inserted" title="按特性组织的目录结构"><a href="guide/styleguide#folders-by-feature-structure">按特性组织的目录结构</a></li><li class="h3 secondary ng-star-inserted" title="应用的根模块"><a href="guide/styleguide#app-root-module">应用的<em>根模块</em></a></li><li class="h3 secondary ng-star-inserted" title="特性模块"><a href="guide/styleguide#feature-modules">特性模块</a></li><li class="h3 secondary ng-star-inserted" title="共享特性模块"><a href="guide/styleguide#shared-feature-module">共享特性模块</a></li><li class="h3 secondary ng-star-inserted" title="惰性加载文件夹"><a href="guide/styleguide#lazy-loaded-folders">惰性加载文件夹</a></li><li class="h3 secondary ng-star-inserted" title="永远不要直接导入惰性加载的目录"><a href="guide/styleguide#never-directly-import-lazy-loaded-folders">永远不要直接导入惰性加载的目录</a></li><li class="h2 secondary ng-star-inserted" title="组件"><a href="guide/styleguide#components">组件</a></li><li class="h3 secondary ng-star-inserted" title="把组件当做元素"><a href="guide/styleguide#components-as-elements">把组件当做元素</a></li><li class="h3 secondary ng-star-inserted" title="把模板和样式提取到它们自己的文件"><a href="guide/styleguide#extract-templates-and-styles-to-their-own-files">把模板和样式提取到它们自己的文件</a></li><li class="h3 secondary ng-star-inserted" title="内联输入和输出属性装饰器"><a href="guide/styleguide#decorate-input-and-output-properties">内联输入和输出属性装饰器</a></li><li class="h3 secondary ng-star-inserted" title="避免为输入和输出属性指定别名"><a href="guide/styleguide#avoid-aliasing-inputs-and-outputs">避免为输入和输出属性指定别名</a></li><li class="h3 secondary ng-star-inserted" title="成员顺序"><a href="guide/styleguide#member-sequence">成员顺序</a></li><li class="h3 secondary ng-star-inserted" title="把逻辑放到服务里"><a href="guide/styleguide#delegate-complex-component-logic-to-services">把逻辑放到服务里</a></li><li class="h3 secondary ng-star-inserted" title="不要给输出属性加前缀"><a href="guide/styleguide#dont-prefix-output-properties">不要给输出属性加前缀</a></li><li class="h3 secondary ng-star-inserted" title="把表现层逻辑放到组件类里"><a href="guide/styleguide#put-presentation-logic-in-the-component-class">把表现层逻辑放到组件类里</a></li><li class="h2 secondary ng-star-inserted" title="指令"><a href="guide/styleguide#directives">指令</a></li><li class="h3 secondary ng-star-inserted" title="使用指令来增强已有元素"><a href="guide/styleguide#use-directives-to-enhance-an-element">使用指令来增强已有元素</a></li><li class="h3 secondary ng-star-inserted" title="HostListener 和 HostBinding 装饰器 vs. 组件元数据 host"><a href="guide/styleguide#hostlistenerhostbinding-decorators-versus-host-metadata"><em>HostListener</em> 和 <em>HostBinding</em> 装饰器 vs. 组件元数据 <em>host</em></a></li><li class="h2 secondary ng-star-inserted" title="服务"><a href="guide/styleguide#services">服务</a></li><li class="h3 secondary ng-star-inserted" title="服务总是单例的"><a href="guide/styleguide#services-are-singletons">服务总是单例的</a></li><li class="h3 secondary ng-star-inserted" title="单一职责"><a href="guide/styleguide#single-responsibility-1">单一职责</a></li><li class="h3 secondary ng-star-inserted" title="提供一个服务"><a href="guide/styleguide#providing-a-service">提供一个服务</a></li><li class="h3 secondary ng-star-inserted" title="使用 @Injectable() 类装饰器"><a href="guide/styleguide#use-the-injectable-class-decorator">使用 @Injectable() 类装饰器</a></li><li class="h2 secondary ng-star-inserted" title="数据服务"><a href="guide/styleguide#data-services">数据服务</a></li><li class="h3 secondary ng-star-inserted" title="通过服务与 Web 服务器通讯"><a href="guide/styleguide#talk-to-the-server-through-a-service">通过服务与 Web 服务器通讯</a></li><li class="h2 secondary ng-star-inserted" title="生命周期钩子"><a href="guide/styleguide#lifecycle-hooks">生命周期钩子</a></li><li class="h3 secondary ng-star-inserted" title="实现生命周期钩子接口"><a href="guide/styleguide#implement-lifecycle-hook-interfaces">实现生命周期钩子接口</a></li><li class="h2 secondary ng-star-inserted" title="附录"><a href="guide/styleguide#appendix">附录</a></li><li class="h3 secondary ng-star-inserted" title="Codelyzer"><a href="guide/styleguide#codelyzer">Codelyzer</a></li><li class="h3 secondary ng-star-inserted" title="文档模板和代码片段"><a href="guide/styleguide#file-templates-and-snippets">文档模板和代码片段</a></li></ul><button type="button" title="Expand/collapse contents" aria-label="Expand/collapse contents" class="toc-more-items embedded material-icons collapsed ng-star-inserted" aria-pressed="false"></button></div></aio-toc><h1 translation-origin="off" id="angular-coding-style-guide">Angular coding style guide<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#angular-coding-style-guide"><i class="material-icons">link</i></a></h1><p translation-result="on">如果你正在寻找关于 Angular 语法、约定和应用组织结构的官方指南，那你就来对了。 本风格指南介绍了提倡的约定，更重要的是，解释了为什么。</p><p translation-origin="off">Looking for an opinionated guide to Angular syntax, conventions, and application structure? Step right in! This style guide presents preferred conventions and, as importantly, explains why.</p><a id="toc"></a><h2 id="style-vocabulary" translation-result="on">风格指南的用词<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-vocabulary"><i class="material-icons">link</i></a></h2><h2 translation-origin="off" id="style-vocabulary">Style vocabulary<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-vocabulary"><i class="material-icons">link</i></a></h2><p translation-result="on">每个指导原则都会描述好的或者坏的做法，所有指导原则都用同样的风格描述。</p><p translation-origin="off">Each guideline describes either a good or bad practice, and all have a consistent presentation.</p><p translation-result="on">指导原则中使用的词汇用来表明推荐的程度。</p><p translation-origin="off">The wording of each guideline indicates how strong the recommendation is.</p><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>意味着总是应该遵循的约定。 说<em>"总是"</em>可能显得有点绝对，应该<em>"总是"</em>遵循的指导原则非常少，不过，只有遇到极不寻常的情况才能打破<em>坚持</em>的原则。</p><p translation-origin="off"><strong>Do</strong> is one that should always be followed. <em>Always</em> might be a bit too strong of a word. Guidelines that literally should always be followed are extremely rare. On the other hand, you need a really unusual case for breaking a <em>Do</em> guideline.</p></div><div class="s-rule consider"><p translation-result="on"><strong>考虑</strong>表示通常应该遵循的指导原则。 如果你能完全理解指导原则背后的含义，并且有很好的理由违反它，那就改吧。但是请保持一致。</p><p translation-origin="off"><strong>Consider</strong> guidelines should generally be followed. If you fully understand the meaning behind the guideline and have a good reason to deviate, then do so. Please strive to be consistent.</p></div><div class="s-rule avoid"><p translation-result="on"><strong>避免</strong>表示你绝对不应该做的事。需要<em>避免</em>的代码范例会有明显的红色标题。</p><p translation-origin="off"><strong>Avoid</strong> indicates something you should almost never do. Code examples to <em>avoid</em> have an unmistakable red header.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong>会给出随后的建议的理由。</p><p translation-origin="off"><strong>Why?</strong> gives reasons for following the previous recommendations.</p></div><h2 id="file-structure-conventions" translation-result="on">文件结构约定<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#file-structure-conventions"><i class="material-icons">link</i></a></h2><h2 translation-origin="off" id="file-structure-conventions">File structure conventions<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#file-structure-conventions"><i class="material-icons">link</i></a></h2><p translation-result="on">在一些代码例子中，有的文件有一个或多个相似名字的配套文件。（例如 hero.component.ts 和 hero.component.html）。</p><p translation-origin="off">Some code examples display a file that has one or more similarly named companion files. For example, <code>hero.component.ts</code> and <code>hero.component.html</code>.</p><p translation-result="on">本指南将会使用像 <code>hero.component.ts|html|css|spec</code> 的简写来表示上面描述的多个文件，目的是保持本指南的简洁性，增加描述文件结构时的可读性。 <a id="single-responsibility"></a></p><p translation-origin="off">The guideline uses the shortcut <code>hero.component.ts|html|css|spec</code> to represent those various files. Using this shortcut makes this guide's file structures easier to read and more terse.</p><h2 id="single-responsibility" translation-result="on">单一职责<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#single-responsibility"><i class="material-icons">link</i></a></h2><h2 translation-origin="off" id="single-responsibility">Single responsibility<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#single-responsibility"><i class="material-icons">link</i></a></h2><p translation-result="on">对所有的组件、服务等等应用<a href="https://wikipedia.org/wiki/Single_responsibility_principle" target="_blank"><i>单一职责原则</i> (SRP)</a>。这样可以让应用更干净、更易读、更易维护、更易测试。</p><p translation-origin="off">Apply the <a href="https://wikipedia.org/wiki/Single_responsibility_principle"><i>single responsibility principle</i> (SRP)</a> to all components, services, and other symbols. This helps make the app cleaner, easier to read and maintain, and more testable.</p><a id="01-01"></a><h3 id="rule-of-one" translation-result="on">单一规则<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#rule-of-one"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="rule-of-one">Rule of One<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#rule-of-one"><i class="material-icons">link</i></a></h3><h4 id="style-01-01" translation-result="on">风格 01-01<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-01-01"><i class="material-icons">link</i></a></h4><h4 translation-origin="off" id="style-01-01">Style 01-01<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-01-01"><i class="material-icons">link</i></a></h4><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>每个文件只定义一样东西（例如服务或组件）。</p><p translation-origin="off"><strong>Do</strong> define one thing, such as a service or component, per file.</p></div><div class="s-rule consider"><p translation-result="on"><strong>考虑</strong>把文件大小限制在 400 行代码以内。</p><p translation-origin="off"><strong>Consider</strong> limiting files to 400 lines of code.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong>单组件文件非常容易阅读、维护，并能防止在版本控制系统里与团队冲突。</p><p translation-origin="off"><strong>Why?</strong> One component per file makes it far easier to read, maintain, and avoid collisions with teams in source control.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong>单组件文件可以防止一些隐蔽的程序缺陷，当把多个组件合写在同一个文件中时，可能造成共享变量、创建意外的闭包，或者与依赖之间产生意外耦合等情况。</p><p translation-origin="off"><strong>Why?</strong> One component per file avoids hidden bugs that often arise when combining components in a file where they may share variables, create unwanted closures, or unwanted coupling with dependencies.</p></div><div class="s-why-last"><p translation-result="on"><strong>为何？</strong>单独的组件通常是该文件默认的导出，可以用路由器实现按需加载。</p><p translation-origin="off"><strong>Why?</strong> A single component can be the default export for its file which facilitates lazy loading with the router.</p></div><p translation-result="on">最关键的是，可以让代码更加可复用、更容易阅读，减少出错的可能性。</p><p translation-origin="off">The key is to make the code more reusable, easier to read, and less mistake prone.</p><p translation-result="on">下面的<em>负面</em>例子定义了 <code>AppComponent</code>，它来引导应用程序，定义了 <code>Hero</code> 模型对象，并从服务器加载了英雄 ... 所有都在同一个文件。 <em>不要这么做</em>。</p><p translation-origin="off">The following <em>negative</em> example defines the <code>AppComponent</code>, bootstraps the app, defines the <code>Hero</code> model object, and loads heroes from the server all in the same file. <em>Don't do this</em>.</p><code-example path="styleguide/src/01-01/app/heroes/hero.component.avoid.ts" header="app/heroes/hero.component.ts" ng-version="9.0.0-rc.11" class="avoidFile"><div style="display:none">/* avoid */ import { <a href="api/core/Component" class="code-anchor">Component</a>, <a href="api/core/NgModule" class="code-anchor">NgModule</a>, <a href="api/core/OnInit" class="code-anchor">OnInit</a> } from '@angular/core'; import { <a href="api/platform-browser/BrowserModule" class="code-anchor">BrowserModule</a> } from '@angular/platform-browser'; import { <a href="api/platform-browser-dynamic/platformBrowserDynamic" class="code-anchor">platformBrowserDynamic</a> } from '@angular/platform-browser-dynamic'; interface Hero { id: number; name: string; } @<a href="api/core/Component" class="code-anchor">Component</a>({ selector: 'app-root', template: ` &lt;h1&gt;{{title}}&lt;/h1&gt; &lt;pre&gt;{{heroes | <a href="api/common/JsonPipe" class="code-anchor">json</a>}}&lt;/pre&gt; `, <a href="api/core/Component#styleUrls" class="code-anchor">styleUrls</a>: ['app/app.component.css'] }) class AppComponent implements <a href="api/core/OnInit" class="code-anchor">OnInit</a> { title = 'Tour of Heroes'; heroes: Hero[] = []; ngOnInit() { getHeroes().then(heroes =&gt; (this.heroes = heroes)); } } @<a href="api/core/NgModule" class="code-anchor">NgModule</a>({ imports: [<a href="api/platform-browser/BrowserModule" class="code-anchor">BrowserModule</a>], declarations: [AppComponent], exports: [AppComponent], bootstrap: [AppComponent] }) export class AppModule {} <a href="api/platform-browser-dynamic/platformBrowserDynamic" class="code-anchor">platformBrowserDynamic</a>().bootstrapModule(AppModule); const HEROES: Hero[] = [ { id: 1, name: 'Bombasto' }, { id: 2, name: 'Tornado' }, { id: 3, name: 'Magneta' } ]; function getHeroes(): Promise&lt;Hero[]&gt; { return Promise.resolve(HEROES); // TODO: get hero data from the server; }</div><header class="ng-star-inserted">app/heroes/hero.component.ts</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from app/heroes/hero.component.ts">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="com">/* avoid */</span><span class="pln">
</span><span class="kwd">import</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><a href="api/core/Component" class="code-anchor"><span class="typ">Component</span></a><span class="pun">,</span><span class="pln"> </span><a href="api/core/NgModule" class="code-anchor"><span class="typ">NgModule</span></a><span class="pun">,</span><span class="pln"> </span><a href="api/core/OnInit" class="code-anchor"><span class="typ">OnInit</span></a><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">from</span><span class="pln"> </span><span class="str">'@angular/core'</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">import</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><a href="api/platform-browser/BrowserModule" class="code-anchor"><span class="typ">BrowserModule</span></a><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">from</span><span class="pln"> </span><span class="str">'@angular/platform-browser'</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">import</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><a href="api/platform-browser-dynamic/platformBrowserDynamic" class="code-anchor"><span class="pln">platformBrowserDynamic</span></a><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">from</span><span class="pln"> </span><span class="str">'@angular/platform-browser-dynamic'</span><span class="pun">;</span><span class="pln">

</span><span class="kwd">interface</span><span class="pln"> </span><span class="typ">Hero</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  id</span><span class="pun">:</span><span class="pln"> number</span><span class="pun">;</span><span class="pln">
  name</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">string</span><span class="pun">;</span><span class="pln">
</span><span class="pun">}</span><span class="pln">

</span><span class="lit">@</span><a href="api/core/Component" class="code-anchor"><span class="lit">Component</span></a><span class="pun">({</span><span class="pln">
  selector</span><span class="pun">:</span><span class="pln"> </span><span class="str">'app-root'</span><span class="pun">,</span><span class="pln">
  </span><span class="kwd">template</span><span class="pun">:</span><span class="pln"> </span><span class="str">`
      &lt;h1&gt;{{title}}&lt;/h1&gt;
      &lt;pre&gt;{{heroes | </span><a href="api/common/JsonPipe" class="code-anchor"><span class="str">json</span></a><span class="str">}}&lt;/pre&gt;
    `</span><span class="pun">,</span><span class="pln">
  </span><a href="api/core/Component#styleUrls" class="code-anchor"><span class="pln">styleUrls</span></a><span class="pun">:</span><span class="pln"> </span><span class="pun">[</span><span class="str">'app/app.component.css'</span><span class="pun">]</span><span class="pln">
</span><span class="pun">})</span><span class="pln">
</span><span class="kwd">class</span><span class="pln"> </span><span class="typ">AppComponent</span><span class="pln"> </span><span class="kwd">implements</span><span class="pln"> </span><a href="api/core/OnInit" class="code-anchor"><span class="typ">OnInit</span></a><span class="pln"> </span><span class="pun">{</span><span class="pln">
  title </span><span class="pun">=</span><span class="pln"> </span><span class="str">'Tour of Heroes'</span><span class="pun">;</span><span class="pln">

  heroes</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Hero</span><span class="pun">[]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> </span><span class="pun">[];</span><span class="pln">

  ngOnInit</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    getHeroes</span><span class="pun">().</span><span class="kwd">then</span><span class="pun">(</span><span class="pln">heroes </span><span class="pun">=&gt;</span><span class="pln"> </span><span class="pun">(</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">heroes </span><span class="pun">=</span><span class="pln"> heroes</span><span class="pun">));</span><span class="pln">
  </span><span class="pun">}</span><span class="pln">
</span><span class="pun">}</span><span class="pln">

</span><span class="lit">@</span><a href="api/core/NgModule" class="code-anchor"><span class="lit">NgModule</span></a><span class="pun">({</span><span class="pln">
  imports</span><span class="pun">:</span><span class="pln"> </span><span class="pun">[</span><a href="api/platform-browser/BrowserModule" class="code-anchor"><span class="typ">BrowserModule</span></a><span class="pun">],</span><span class="pln">
  declarations</span><span class="pun">:</span><span class="pln"> </span><span class="pun">[</span><span class="typ">AppComponent</span><span class="pun">],</span><span class="pln">
  exports</span><span class="pun">:</span><span class="pln"> </span><span class="pun">[</span><span class="typ">AppComponent</span><span class="pun">],</span><span class="pln">
  bootstrap</span><span class="pun">:</span><span class="pln"> </span><span class="pun">[</span><span class="typ">AppComponent</span><span class="pun">]</span><span class="pln">
</span><span class="pun">})</span><span class="pln">
</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">AppModule</span><span class="pln"> </span><span class="pun">{}</span><span class="pln">

</span><a href="api/platform-browser-dynamic/platformBrowserDynamic" class="code-anchor"><span class="pln">platformBrowserDynamic</span></a><span class="pun">().</span><span class="pln">bootstrapModule</span><span class="pun">(</span><span class="typ">AppModule</span><span class="pun">);</span><span class="pln">

</span><span class="kwd">const</span><span class="pln"> HEROES</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Hero</span><span class="pun">[]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> </span><span class="pun">[</span><span class="pln">
  </span><span class="pun">{</span><span class="pln"> id</span><span class="pun">:</span><span class="pln"> </span><span class="lit">1</span><span class="pun">,</span><span class="pln"> name</span><span class="pun">:</span><span class="pln"> </span><span class="str">'Bombasto'</span><span class="pln"> </span><span class="pun">},</span><span class="pln">
  </span><span class="pun">{</span><span class="pln"> id</span><span class="pun">:</span><span class="pln"> </span><span class="lit">2</span><span class="pun">,</span><span class="pln"> name</span><span class="pun">:</span><span class="pln"> </span><span class="str">'Tornado'</span><span class="pln"> </span><span class="pun">},</span><span class="pln">
  </span><span class="pun">{</span><span class="pln"> id</span><span class="pun">:</span><span class="pln"> </span><span class="lit">3</span><span class="pun">,</span><span class="pln"> name</span><span class="pun">:</span><span class="pln"> </span><span class="str">'Magneta'</span><span class="pln"> </span><span class="pun">}</span><span class="pln">
</span><span class="pun">];</span><span class="pln">

</span><span class="kwd">function</span><span class="pln"> getHeroes</span><span class="pun">():</span><span class="pln"> </span><span class="typ">Promise</span><span class="pun">&lt;</span><span class="typ">Hero</span><span class="pun">[]&gt;</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  </span><span class="kwd">return</span><span class="pln"> </span><span class="typ">Promise</span><span class="pun">.</span><span class="pln">resolve</span><span class="pun">(</span><span class="pln">HEROES</span><span class="pun">);</span><span class="pln"> </span><span class="com">// TODO: get hero data from the server;</span><span class="pln">
</span><span class="pun">}</span></code>
    </pre></aio-code></code-example><p translation-result="on">最好将组件及其支撑部件重新分配到独立的文件。</p><p translation-origin="off">It is a better practice to redistribute the component and its supporting classes into their own, dedicated files.</p><code-tabs ng-version="9.0.0-rc.11"><div style="display:none"><code-pane header="main.ts" path="styleguide/src/01-01/main.ts">import { <a href="api/platform-browser-dynamic/platformBrowserDynamic" class="code-anchor">platformBrowserDynamic</a> } from '@angular/platform-browser-dynamic'; import { AppModule } from './app/app.module'; <a href="api/platform-browser-dynamic/platformBrowserDynamic" class="code-anchor">platformBrowserDynamic</a>().bootstrapModule(AppModule);</code-pane><code-pane header="app/app.module.ts" path="styleguide/src/01-01/app/app.module.ts">import { <a href="api/core/NgModule" class="code-anchor">NgModule</a> } from '@angular/core'; import { <a href="api/platform-browser/BrowserModule" class="code-anchor">BrowserModule</a> } from '@angular/platform-browser'; import { <a href="api/router/RouterModule" class="code-anchor">RouterModule</a> } from '@angular/router'; import { AppComponent } from './app.component'; import { HeroesComponent } from './heroes/heroes.component'; @<a href="api/core/NgModule" class="code-anchor">NgModule</a>({ imports: [ <a href="api/platform-browser/BrowserModule" class="code-anchor">BrowserModule</a>, ], declarations: [ AppComponent, HeroesComponent ], exports: [ AppComponent ], bootstrap: [ AppComponent ] }) export class AppModule { }</code-pane><code-pane header="app/app.component.ts" path="styleguide/src/01-01/app/app.component.ts">import { <a href="api/core/Component" class="code-anchor">Component</a> } from '@angular/core'; import { HeroService } from './heroes'; @<a href="api/core/Component" class="code-anchor">Component</a>({ selector: 'toh-app', template: ` &lt;toh-heroes&gt;&lt;/toh-heroes&gt; `, <a href="api/core/Component#styleUrls" class="code-anchor">styleUrls</a>: ['./app.component.css'], providers: [HeroService] }) export class AppComponent {}</code-pane><code-pane header="app/heroes/heroes.component.ts" path="styleguide/src/01-01/app/heroes/heroes.component.ts">import { <a href="api/core/Component" class="code-anchor">Component</a>, <a href="api/core/OnInit" class="code-anchor">OnInit</a> } from '@angular/core'; import { Hero, HeroService } from './shared'; @<a href="api/core/Component" class="code-anchor">Component</a>({ selector: 'toh-heroes', template: ` &lt;pre&gt;{{heroes | <a href="api/common/JsonPipe" class="code-anchor">json</a>}}&lt;/pre&gt; ` }) export class HeroesComponent implements <a href="api/core/OnInit" class="code-anchor">OnInit</a> { heroes: Hero[] = []; constructor(private heroService: HeroService) {} ngOnInit() { this.heroService.getHeroes() .then(heroes =&gt; this.heroes = heroes); } }</code-pane><code-pane header="app/heroes/shared/hero.service.ts" path="styleguide/src/01-01/app/heroes/shared/hero.service.ts">import { <a href="api/core/Injectable" class="code-anchor">Injectable</a> } from '@angular/core'; import { HEROES } from './mock-heroes'; @<a href="api/core/Injectable" class="code-anchor">Injectable</a>() export class HeroService { getHeroes() { return Promise.resolve(HEROES); } }</code-pane><code-pane header="app/heroes/shared/hero.model.ts" path="styleguide/src/01-01/app/heroes/shared/hero.model.ts">export interface Hero { id: number; name: string; }</code-pane><code-pane header="app/heroes/shared/mock-heroes.ts" path="styleguide/src/01-01/app/heroes/shared/mock-heroes.ts">import { Hero } from './hero.model'; export const HEROES: Hero[] = [ { id: 1, name: 'Bombasto' }, { id: 2, name: 'Tornado' }, { id: 3, name: 'Magneta' } ];</code-pane></div><mat-card class="mat-card"><mat-tab-group class="mat-tab-group code-tab-group mat-primary"><mat-tab-header class="mat-tab-header mat-tab-header-pagination-controls-enabled"><div aria-hidden="true" mat-ripple="" class="mat-ripple mat-tab-header-pagination mat-tab-header-pagination-before mat-elevation-z4 mat-tab-header-pagination-disabled"><div class="mat-tab-header-pagination-chevron"></div></div><div class="mat-tab-label-container"><div role="tablist" class="mat-tab-list" style="transform:translateX(0)"><div class="mat-tab-labels"><div role="tab" mattablabelwrapper="" mat-ripple="" cdkmonitorelementfocus="" class="mat-ripple mat-tab-label mat-tab-label-active ng-star-inserted" id="mat-tab-label-0-0" tabindex="0" aria-posinset="1" aria-setsize="7" aria-controls="mat-tab-content-0-0" aria-selected="true" aria-disabled="false"><div class="mat-tab-label-content"><span class="ng-star-inserted">main.ts</span></div></div><div role="tab" mattablabelwrapper="" mat-ripple="" cdkmonitorelementfocus="" class="mat-ripple mat-tab-label ng-star-inserted" id="mat-tab-label-0-1" tabindex="-1" aria-posinset="2" aria-setsize="7" aria-controls="mat-tab-content-0-1" aria-selected="false" aria-disabled="false"><div class="mat-tab-label-content"><span class="ng-star-inserted">app/app.module.ts</span></div></div><div role="tab" mattablabelwrapper="" mat-ripple="" cdkmonitorelementfocus="" class="mat-ripple mat-tab-label ng-star-inserted" id="mat-tab-label-0-2" tabindex="-1" aria-posinset="3" aria-setsize="7" aria-controls="mat-tab-content-0-2" aria-selected="false" aria-disabled="false"><div class="mat-tab-label-content"><span class="ng-star-inserted">app/app.component.ts</span></div></div><div role="tab" mattablabelwrapper="" mat-ripple="" cdkmonitorelementfocus="" class="mat-ripple mat-tab-label ng-star-inserted" id="mat-tab-label-0-3" tabindex="-1" aria-posinset="4" aria-setsize="7" aria-controls="mat-tab-content-0-3" aria-selected="false" aria-disabled="false"><div class="mat-tab-label-content"><span class="ng-star-inserted">app/heroes/heroes.component.ts</span></div></div><div role="tab" mattablabelwrapper="" mat-ripple="" cdkmonitorelementfocus="" class="mat-ripple mat-tab-label ng-star-inserted" id="mat-tab-label-0-4" tabindex="-1" aria-posinset="5" aria-setsize="7" aria-controls="mat-tab-content-0-4" aria-selected="false" aria-disabled="false"><div class="mat-tab-label-content"><span class="ng-star-inserted">app/heroes/shared/hero.service.ts</span></div></div><div role="tab" mattablabelwrapper="" mat-ripple="" cdkmonitorelementfocus="" class="mat-ripple mat-tab-label ng-star-inserted" id="mat-tab-label-0-5" tabindex="-1" aria-posinset="6" aria-setsize="7" aria-controls="mat-tab-content-0-5" aria-selected="false" aria-disabled="false"><div class="mat-tab-label-content"><span class="ng-star-inserted">app/heroes/shared/hero.model.ts</span></div></div><div role="tab" mattablabelwrapper="" mat-ripple="" cdkmonitorelementfocus="" class="mat-ripple mat-tab-label ng-star-inserted" id="mat-tab-label-0-6" tabindex="-1" aria-posinset="7" aria-setsize="7" aria-controls="mat-tab-content-0-6" aria-selected="false" aria-disabled="false"><div class="mat-tab-label-content"><span class="ng-star-inserted">app/heroes/shared/mock-heroes.ts</span></div></div></div><mat-ink-bar class="mat-ink-bar" style="visibility:visible;left:0;width:160px"></mat-ink-bar></div></div><div aria-hidden="true" mat-ripple="" class="mat-ripple mat-tab-header-pagination mat-tab-header-pagination-after mat-elevation-z4"><div class="mat-tab-header-pagination-chevron"></div></div></mat-tab-header><div class="mat-tab-body-wrapper"><mat-tab-body role="tabpanel" class="mat-tab-body ng-tns-c50-2 mat-tab-body-active ng-star-inserted" id="mat-tab-content-0-0" aria-labelledby="mat-tab-label-0-0"><div class="mat-tab-body-content ng-tns-c50-2 ng-trigger ng-trigger-translateTab" style="transform:none"><aio-code class="ng-star-inserted" style=""><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from main.ts">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="kwd">import</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><a href="api/platform-browser-dynamic/platformBrowserDynamic" class="code-anchor"><span class="pln">platformBrowserDynamic</span></a><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">from</span><span class="pln"> </span><span class="str">'@angular/platform-browser-dynamic'</span><span class="pun">;</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="typ">AppModule</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">from</span><span class="pln"> </span><span class="str">'./app/app.module'</span><span class="pun">;</span><span class="pln">

</span><a href="api/platform-browser-dynamic/platformBrowserDynamic" class="code-anchor"><span class="pln">platformBrowserDynamic</span></a><span class="pun">().</span><span class="pln">bootstrapModule</span><span class="pun">(</span><span class="typ">AppModule</span><span class="pun">);</span></code>
    </pre></aio-code></div></mat-tab-body><mat-tab-body role="tabpanel" class="mat-tab-body ng-tns-c50-3 ng-star-inserted" id="mat-tab-content-0-1" aria-labelledby="mat-tab-label-0-1"><div class="mat-tab-body-content ng-tns-c50-3 ng-trigger ng-trigger-translateTab" style="transform:translate3d(100%,0,0);min-height:1px"></div></mat-tab-body><mat-tab-body role="tabpanel" class="mat-tab-body ng-tns-c50-4 ng-star-inserted" id="mat-tab-content-0-2" aria-labelledby="mat-tab-label-0-2"><div class="mat-tab-body-content ng-tns-c50-4 ng-trigger ng-trigger-translateTab" style="transform:translate3d(100%,0,0);min-height:1px"></div></mat-tab-body><mat-tab-body role="tabpanel" class="mat-tab-body ng-tns-c50-5 ng-star-inserted" id="mat-tab-content-0-3" aria-labelledby="mat-tab-label-0-3"><div class="mat-tab-body-content ng-tns-c50-5 ng-trigger ng-trigger-translateTab" style="transform:translate3d(100%,0,0);min-height:1px"></div></mat-tab-body><mat-tab-body role="tabpanel" class="mat-tab-body ng-tns-c50-6 ng-star-inserted" id="mat-tab-content-0-4" aria-labelledby="mat-tab-label-0-4"><div class="mat-tab-body-content ng-tns-c50-6 ng-trigger ng-trigger-translateTab" style="transform:translate3d(100%,0,0);min-height:1px"></div></mat-tab-body><mat-tab-body role="tabpanel" class="mat-tab-body ng-tns-c50-7 ng-star-inserted" id="mat-tab-content-0-5" aria-labelledby="mat-tab-label-0-5"><div class="mat-tab-body-content ng-tns-c50-7 ng-trigger ng-trigger-translateTab" style="transform:translate3d(100%,0,0);min-height:1px"></div></mat-tab-body><mat-tab-body role="tabpanel" class="mat-tab-body ng-tns-c50-8 ng-star-inserted" id="mat-tab-content-0-6" aria-labelledby="mat-tab-label-0-6"><div class="mat-tab-body-content ng-tns-c50-8 ng-trigger ng-trigger-translateTab" style="transform:translate3d(100%,0,0);min-height:1px"></div></mat-tab-body></div></mat-tab-group></mat-card></code-tabs><p translation-result="on">随着应用程序的成长，本法则会变得越来越重要。</p><p translation-origin="off">As the app grows, this rule becomes even more important.</p><p translation-result="on"><a href="guide/styleguide#toc">回到顶部</a></p><p translation-origin="off"><a href="guide/styleguide#toc">Back to top</a></p><a id="01-02"></a><h3 id="small-functions" translation-result="on">小函数<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#small-functions"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="small-functions">Small functions<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#small-functions"><i class="material-icons">link</i></a></h3><h4 id="style-01-02" translation-result="on">风格 01-02<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-01-02"><i class="material-icons">link</i></a></h4><h4 translation-origin="off" id="style-01-02">Style 01-02<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-01-02"><i class="material-icons">link</i></a></h4><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>定义简单函数</p><p translation-origin="off"><strong>Do</strong> define small functions</p></div><div class="s-rule consider"><p translation-result="on"><strong>考虑</strong>限制在 75 行之内。</p><p translation-origin="off"><strong>Consider</strong> limiting to no more than 75 lines.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong>简单函数更易于测试，特别是当它们只做一件事，只为一个目的服务时。</p><p translation-origin="off"><strong>Why?</strong> Small functions are easier to test, especially when they do one thing and serve one purpose.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong>简单函数促进代码重用。</p><p translation-origin="off"><strong>Why?</strong> Small functions promote reuse.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong>简单函数更易于阅读。</p><p translation-origin="off"><strong>Why?</strong> Small functions are easier to read.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong>简单函数更易于维护。</p><p translation-origin="off"><strong>Why?</strong> Small functions are easier to maintain.</p></div><div class="s-why-last"><p translation-result="on"><strong>为何？</strong>小函数可避免易在大函数中产生的隐蔽性错误，例如与外界共享变量、创建意外的闭包或与依赖之间产生意外耦合等。</p><p translation-origin="off"><strong>Why?</strong> Small functions help avoid hidden bugs that come with large functions that share variables with external scope, create unwanted closures, or unwanted coupling with dependencies.</p></div><p translation-result="on"><a href="guide/styleguide#toc">回到顶部</a></p><p translation-origin="off"><a href="guide/styleguide#toc">Back to top</a></p><h2 id="naming" translation-result="on">命名<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#naming"><i class="material-icons">link</i></a></h2><h2 translation-origin="off" id="naming">Naming<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#naming"><i class="material-icons">link</i></a></h2><p translation-result="on">命名约定对可维护性和可读性非常重要。本指南为文件名和符号名推荐了一套命名约定。</p><p translation-origin="off">Naming conventions are hugely important to maintainability and readability. This guide recommends naming conventions for the file name and the symbol name.</p><a id="02-01"></a><h3 id="general-naming-guidelines" translation-result="on">总体命名原则<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#general-naming-guidelines"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="general-naming-guidelines">General Naming Guidelines<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#general-naming-guidelines"><i class="material-icons">link</i></a></h3><h4 id="style-02-01" translation-result="on">风格 02-01<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-02-01"><i class="material-icons">link</i></a></h4><h4 translation-origin="off" id="style-02-01">Style 02-01<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-02-01"><i class="material-icons">link</i></a></h4><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>所有符号使用一致的命名规则。</p><p translation-origin="off"><strong>Do</strong> use consistent names for all symbols.</p></div><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>遵循同一个模式来描述符号的特性和类型。推荐的模式为 <code>feature.type.ts</code>。</p><p translation-origin="off"><strong>Do</strong> follow a pattern that describes the symbol's feature then its type. The recommended pattern is <code>feature.type.ts</code>.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong>命名约定提供了一致的方式来查找内容，让你一眼就能找到。 项目的一致性是至关重要的。团队内的一致性也很重要。整个公司的一致性会提供惊人的效率。</p><p translation-origin="off"><strong>Why?</strong> Naming conventions help provide a consistent way to find content at a glance. Consistency within the project is vital. Consistency with a team is important. Consistency across a company provides tremendous efficiency.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong>命名约定帮助你更快得找到想找的代码，也更容易理解它。</p><p translation-origin="off"><strong>Why?</strong> The naming conventions should simply help find desired code faster and make it easier to understand.</p></div><div class="s-why-last"><p translation-result="on"><strong>为何？</strong>目录名和文件名应该清楚的传递它们的意图。 例如，<code>app/heroes/hero-list.component.ts</code> 包含了一个用来管理英雄列表的组件。</p><p translation-origin="off"><strong>Why?</strong> Names of folders and files should clearly convey their intent. For example, <code>app/heroes/hero-list.component.ts</code> may contain a component that manages a list of heroes.</p></div><p translation-result="on"><a href="guide/styleguide#toc">回到顶部</a></p><p translation-origin="off"><a href="guide/styleguide#toc">Back to top</a></p><a id="02-02"></a><h3 id="separate-file-names-with-dots-and-dashes" translation-result="on">使用点和横杠来分隔文件名<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#separate-file-names-with-dots-and-dashes"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="separate-file-names-with-dots-and-dashes">Separate file names with dots and dashes<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#separate-file-names-with-dots-and-dashes"><i class="material-icons">link</i></a></h3><h4 id="style-02-02" translation-result="on">风格 02-02<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-02-02"><i class="material-icons">link</i></a></h4><h4 translation-origin="off" id="style-02-02">Style 02-02<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-02-02"><i class="material-icons">link</i></a></h4><div class="s-rule do"><p translation-result="on"><strong>坚持</strong> 在描述性名字中，用横杠来分隔单词。</p><p translation-origin="off"><strong>Do</strong> use dashes to separate words in the descriptive name.</p></div><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>使用点来分隔描述性名字和类型。</p><p translation-origin="off"><strong>Do</strong> use dots to separate the descriptive name from the type.</p></div><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>遵循先描述组件特性，再描述它的类型的模式，对所有组件使用一致的类型命名规则。推荐的模式为 <code>feature.type.ts</code>。</p><p translation-origin="off"><strong>Do</strong> use consistent type names for all components following a pattern that describes the component's feature then its type. A recommended pattern is <code>feature.type.ts</code>.</p></div><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>使用惯用的后缀来描述类型，包括 <code>*.service</code>、<code>*.component</code>、<code>*.pipe</code>、<code>.module</code>、<code>.directive</code>。 必要时可以创建更多类型名，但必须注意，不要创建太多。</p><p translation-origin="off"><strong>Do</strong> use conventional type names including <code>.service</code>, <code>.component</code>, <code>.pipe</code>, <code>.module</code>, and <code>.directive</code>. Invent additional type names if you must but take care not to create too many.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong>类型名字提供一致的方式来快速的识别文件中有什么。</p><p translation-origin="off"><strong>Why?</strong> Type names provide a consistent way to quickly identify what is in the file.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong> 类型名可以让你轻松利用编辑器或者 IDE 的模糊搜索功能找到特定文件类型。</p><p translation-origin="off"><strong>Why?</strong> Type names make it easy to find a specific file type using an editor or IDE's fuzzy search techniques.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong> 像 <code>.service</code> 这样的没有简写过的类型名字，描述清楚，毫不含糊。 像 <code>.srv</code>, <code>.svc</code>, 和 <code>.serv</code> 这样的简写可能令人困惑。</p><p translation-origin="off"><strong>Why?</strong> Unabbreviated type names such as <code>.service</code> are descriptive and unambiguous. Abbreviations such as <code>.srv</code>, <code>.svc</code>, and <code>.serv</code> can be confusing.</p></div><div class="s-why-last"><p translation-result="on"><strong>为何？</strong>为自动化任务提供模式匹配。</p><p translation-origin="off"><strong>Why?</strong> Type names provide pattern matching for any automated tasks.</p></div><p translation-result="on"><a href="guide/styleguide#toc">回到顶部</a></p><p translation-origin="off"><a href="guide/styleguide#toc">Back to top</a></p><a id="02-03"></a><h3 id="symbols-and-file-names" translation-result="on">符号名与文件名<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#symbols-and-file-names"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="symbols-and-file-names">Symbols and file names<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#symbols-and-file-names"><i class="material-icons">link</i></a></h3><h4 id="style-02-03" translation-result="on">风格 02-03<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-02-03"><i class="material-icons">link</i></a></h4><h4 translation-origin="off" id="style-02-03">Style 02-03<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-02-03"><i class="material-icons">link</i></a></h4><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>为所有东西使用一致的命名约定，以它们所代表的东西命名。</p><p translation-origin="off"><strong>Do</strong> use consistent names for all assets named after what they represent.</p></div><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>使用大写驼峰命名法来命名类。符号名匹配它所在的文件名。</p><p translation-origin="off"><strong>Do</strong> use upper camel case for class names.</p></div><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>在符号名后面追加约定的类型后缀（例如 <code><a href="api/core/Component" class="code-anchor">Component</a></code>、<code><a href="api/core/Directive" class="code-anchor">Directive</a></code>、<code>Module</code>、<code><a href="api/core/Pipe" class="code-anchor">Pipe</a></code>、<code>Service</code>）。</p><p translation-origin="off"><strong>Do</strong> match the name of the symbol to the name of the file.</p></div><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>在符号名后面追加约定的类型后缀（例如 <code>.component.ts</code>、<code>.directive.ts</code>、<code>.module.ts</code>、<code>.pipe.ts</code>、<code>.service.ts</code>）。</p><p translation-origin="off"><strong>Do</strong> append the symbol name with the conventional suffix (such as <code><a href="api/core/Component" class="code-anchor">Component</a></code>, <code><a href="api/core/Directive" class="code-anchor">Directive</a></code>, <code>Module</code>, <code><a href="api/core/Pipe" class="code-anchor">Pipe</a></code>, or <code>Service</code>) for a thing of that type.</p></div><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>在文件名后面追加约定的类型后缀（例如 <code>.component.ts</code>、<code>.directive.ts</code>、<code>.module.ts</code>、<code>.pipe.ts</code>、<code>.service.ts</code>）。</p><p translation-origin="off"><strong>Do</strong> give the filename the conventional suffix (such as <code>.component.ts</code>, <code>.directive.ts</code>, <code>.module.ts</code>, <code>.pipe.ts</code>, or <code>.service.ts</code>) for a file of that type.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong>遵循一致的约定可以快速识别和引用不同类型的资产。</p><p translation-origin="off"><strong>Why?</strong> Consistent conventions make it easy to quickly identify and reference assets of different types.</p></div><table width="100%"><colgroup><col width="50%"><col width="50%"></colgroup><tbody><tr><th><p translation-result="on">符号名</p><p translation-origin="off">Symbol Name</p></th><th><p translation-result="on">文件名</p><p translation-origin="off">File Name</p></th></tr><tr style=""><td><code-example hidecopy="" class="no-box" ng-version="9.0.0-rc.11"><div style="display:none">@<a href="api/core/Component" class="code-anchor">Component</a>({ ... }) export class AppComponent { }</div><aio-code><pre class="lang- prettyprint">      
      <code class="animated fadeIn"><span class="lit">@</span><a href="api/core/Component" class="code-anchor"><span class="lit">Component</span></a><span class="pun">({</span><span class="pln"> </span><span class="pun">...</span><span class="pln"> </span><span class="pun">})</span><span class="pln">
</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">AppComponent</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="pun">}</span></code>
    </pre></aio-code></code-example></td><td><p>app.component.ts</p></td></tr><tr style=""><td><code-example hidecopy="" class="no-box" ng-version="9.0.0-rc.11"><div style="display:none">@<a href="api/core/Component" class="code-anchor">Component</a>({ ... }) export class HeroesComponent { }</div><aio-code><pre class="lang- prettyprint">      
      <code class="animated fadeIn"><span class="lit">@</span><a href="api/core/Component" class="code-anchor"><span class="lit">Component</span></a><span class="pun">({</span><span class="pln"> </span><span class="pun">...</span><span class="pln"> </span><span class="pun">})</span><span class="pln">
</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">HeroesComponent</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="pun">}</span></code>
    </pre></aio-code></code-example></td><td><p>heroes.component.ts</p></td></tr><tr style=""><td><code-example hidecopy="" class="no-box" ng-version="9.0.0-rc.11"><div style="display:none">@<a href="api/core/Component" class="code-anchor">Component</a>({ ... }) export class HeroListComponent { }</div><aio-code><pre class="lang- prettyprint">      
      <code class="animated fadeIn"><span class="lit">@</span><a href="api/core/Component" class="code-anchor"><span class="lit">Component</span></a><span class="pun">({</span><span class="pln"> </span><span class="pun">...</span><span class="pln"> </span><span class="pun">})</span><span class="pln">
</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">HeroListComponent</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="pun">}</span></code>
    </pre></aio-code></code-example></td><td><p>hero-list.component.ts</p></td></tr><tr style=""><td><code-example hidecopy="" class="no-box" ng-version="9.0.0-rc.11"><div style="display:none">@<a href="api/core/Component" class="code-anchor">Component</a>({ ... }) export class HeroDetailComponent { }</div><aio-code><pre class="lang- prettyprint">      
      <code class="animated fadeIn"><span class="lit">@</span><a href="api/core/Component" class="code-anchor"><span class="lit">Component</span></a><span class="pun">({</span><span class="pln"> </span><span class="pun">...</span><span class="pln"> </span><span class="pun">})</span><span class="pln">
</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">HeroDetailComponent</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="pun">}</span></code>
    </pre></aio-code></code-example></td><td><p>hero-detail.component.ts</p></td></tr><tr style=""><td><code-example hidecopy="" class="no-box" ng-version="9.0.0-rc.11"><div style="display:none">@<a href="api/core/Directive" class="code-anchor">Directive</a>({ ... }) export class ValidationDirective { }</div><aio-code><pre class="lang- prettyprint">      
      <code class="animated fadeIn"><span class="lit">@</span><a href="api/core/Directive" class="code-anchor"><span class="lit">Directive</span></a><span class="pun">({</span><span class="pln"> </span><span class="pun">...</span><span class="pln"> </span><span class="pun">})</span><span class="pln">
</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">ValidationDirective</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="pun">}</span></code>
    </pre></aio-code></code-example></td><td><p>validation.directive.ts</p></td></tr><tr style=""><td><code-example hidecopy="" class="no-box" ng-version="9.0.0-rc.11"><div style="display:none">@<a href="api/core/NgModule" class="code-anchor">NgModule</a>({ ... }) export class AppModule</div><aio-code><pre class="lang- prettyprint">      
      <code class="animated fadeIn"><span class="lit">@</span><a href="api/core/NgModule" class="code-anchor"><span class="lit">NgModule</span></a><span class="pun">({</span><span class="pln"> </span><span class="pun">...</span><span class="pln"> </span><span class="pun">})</span><span class="pln">
</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">AppModule</span></code>
    </pre></aio-code></code-example></td><td><p>app.module.ts</p></td></tr><tr style=""><td><code-example hidecopy="" class="no-box" ng-version="9.0.0-rc.11"><div style="display:none">@<a href="api/core/Pipe" class="code-anchor">Pipe</a>({ name: 'initCaps' }) export class InitCapsPipe implements <a href="api/core/PipeTransform" class="code-anchor">PipeTransform</a> { }</div><aio-code><pre class="lang- prettyprint">      
      <code class="animated fadeIn"><span class="lit">@</span><a href="api/core/Pipe" class="code-anchor"><span class="lit">Pipe</span></a><span class="pun">({</span><span class="pln"> name</span><span class="pun">:</span><span class="pln"> </span><span class="str">'initCaps'</span><span class="pln"> </span><span class="pun">})</span><span class="pln">
</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">InitCapsPipe</span><span class="pln"> </span><span class="kwd">implements</span><span class="pln"> </span><a href="api/core/PipeTransform" class="code-anchor"><span class="typ">PipeTransform</span></a><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="pun">}</span></code>
    </pre></aio-code></code-example></td><td><p>init-caps.pipe.ts</p></td></tr><tr style=""><td><code-example hidecopy="" class="no-box" ng-version="9.0.0-rc.11"><div style="display:none">@<a href="api/core/Injectable" class="code-anchor">Injectable</a>() export class UserProfileService { }</div><aio-code><pre class="lang- prettyprint">      
      <code class="animated fadeIn"><span class="lit">@</span><a href="api/core/Injectable" class="code-anchor"><span class="lit">Injectable</span></a><span class="pun">()</span><span class="pln">
</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">UserProfileService</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="pun">}</span></code>
    </pre></aio-code></code-example></td><td><p>user-profile.service.ts</p></td></tr></tbody></table><p translation-result="on"><a href="guide/styleguide#toc">回到顶部</a></p><p translation-origin="off"><a href="guide/styleguide#toc">Back to top</a></p><a id="02-04"></a><h3 id="service-names" translation-result="on">服务名<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#service-names"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="service-names">Service names<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#service-names"><i class="material-icons">link</i></a></h3><h4 id="style-02-04" translation-result="on">风格 02-04<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-02-04"><i class="material-icons">link</i></a></h4><h4 translation-origin="off" id="style-02-04">Style 02-04<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-02-04"><i class="material-icons">link</i></a></h4><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>使用一致的规则命名服务，以它们的特性来命名。</p><p translation-origin="off"><strong>Do</strong> use consistent names for all services named after their feature.</p></div><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>为服务的类名加上 <code>Service</code> 后缀。 例如，获取数据或英雄列表的服务应该命名为 <code>DataService</code> 或 <code>HeroService</code>。</p><p translation-origin="off"><strong>Do</strong> suffix a service class name with <code>Service</code>. For example, something that gets data or heroes should be called a <code>DataService</code> or a <code>HeroService</code>.</p><p translation-result="on">有些词汇显然就是服务，比如那些以“-er”后缀结尾的。比如把记日志的服务命名为 <code>Logger</code> 就比 <code>LoggerService</code> 更好些。需要在你的项目中决定这种特例是否可以接受。 但无论如何，都要尽量保持一致。</p><p translation-origin="off">A few terms are unambiguously services. They typically indicate agency by ending in "-er". You may prefer to name a service that logs messages <code>Logger</code> rather than <code>LoggerService</code>. Decide if this exception is agreeable in your project. As always, strive for consistency.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong>提供一致的方式来快速识别和引用服务。</p><p translation-origin="off"><strong>Why?</strong> Provides a consistent way to quickly identify and reference services.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong>像 <code>Logger</code> 这样的清楚的服务名不需要后缀。</p><p translation-origin="off"><strong>Why?</strong> Clear service names such as <code>Logger</code> do not require a suffix.</p></div><div class="s-why-last"><p translation-result="on"><strong>为何？</strong>像 <code>Credit</code> 这样的，服务名是名词，需要一个后缀。当不能明显分辨它是服务还是其它东西时，应该添加后缀。</p><p translation-origin="off"><strong>Why?</strong> Service names such as <code>Credit</code> are nouns and require a suffix and should be named with a suffix when it is not obvious if it is a service or something else.</p></div><table width="100%"><colgroup><col width="50%"><col width="50%"></colgroup><tbody><tr><th><p translation-result="on">符号名</p><p translation-origin="off">Symbol Name</p></th><th><p translation-result="on">文件名</p><p translation-origin="off">File Name</p></th></tr><tr style=""><td><code-example hidecopy="" class="no-box" ng-version="9.0.0-rc.11"><div style="display:none">@<a href="api/core/Injectable" class="code-anchor">Injectable</a>() export class HeroDataService { }</div><aio-code><pre class="lang- prettyprint">      
      <code class="animated fadeIn"><span class="lit">@</span><a href="api/core/Injectable" class="code-anchor"><span class="lit">Injectable</span></a><span class="pun">()</span><span class="pln">
</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">HeroDataService</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="pun">}</span></code>
    </pre></aio-code></code-example></td><td><p>hero-data.service.ts</p></td></tr><tr style=""><td><code-example hidecopy="" class="no-box" ng-version="9.0.0-rc.11"><div style="display:none">@<a href="api/core/Injectable" class="code-anchor">Injectable</a>() export class CreditService { }</div><aio-code><pre class="lang- prettyprint">      
      <code class="animated fadeIn"><span class="lit">@</span><a href="api/core/Injectable" class="code-anchor"><span class="lit">Injectable</span></a><span class="pun">()</span><span class="pln">
</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">CreditService</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="pun">}</span></code>
    </pre></aio-code></code-example></td><td><p>credit.service.ts</p></td></tr><tr style=""><td><code-example hidecopy="" class="no-box" ng-version="9.0.0-rc.11"><div style="display:none">@<a href="api/core/Injectable" class="code-anchor">Injectable</a>() export class Logger { }</div><aio-code><pre class="lang- prettyprint">      
      <code class="animated fadeIn"><span class="lit">@</span><a href="api/core/Injectable" class="code-anchor"><span class="lit">Injectable</span></a><span class="pun">()</span><span class="pln">
</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">Logger</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="pun">}</span></code>
    </pre></aio-code></code-example></td><td><p>logger.service.ts</p></td></tr></tbody></table><p translation-result="on"><a href="guide/styleguide#toc">回到顶部</a></p><p translation-origin="off"><a href="guide/styleguide#toc">Back to top</a></p><a id="02-05"></a><h3 id="bootstrapping" translation-result="on">引导<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#bootstrapping"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="bootstrapping">Bootstrapping<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#bootstrapping"><i class="material-icons">link</i></a></h3><h4 id="style-02-05" translation-result="on">风格 02-05<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-02-05"><i class="material-icons">link</i></a></h4><h4 translation-origin="off" id="style-02-05">Style 02-05<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-02-05"><i class="material-icons">link</i></a></h4><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>把应用的引导程序和平台相关的逻辑放到名为 <code>main.ts</code> 的文件里。</p><p translation-origin="off"><strong>Do</strong> put bootstrapping and platform logic for the app in a file named <code>main.ts</code>.</p></div><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>在引导逻辑中包含错误处理代码。</p><p translation-origin="off"><strong>Do</strong> include error handling in the bootstrapping logic.</p></div><div class="s-rule avoid"><p translation-result="on"><strong>避免</strong>把应用逻辑放在 <code>main.ts</code> 中，而应放在组件或服务里。</p><p translation-origin="off"><strong>Avoid</strong> putting app logic in <code>main.ts</code>. Instead, consider placing it in a component or service.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong>应用的启动逻辑遵循一致的约定。</p><p translation-origin="off"><strong>Why?</strong> Follows a consistent convention for the startup logic of an app.</p></div><div class="s-why-last"><p translation-result="on"><strong>为何？</strong>这是从其它技术平台借鉴的常用约定。</p><p translation-origin="off"><strong>Why?</strong> Follows a familiar convention from other technology platforms.</p></div><code-example path="styleguide/src/02-05/main.ts" header="main.ts" ng-version="9.0.0-rc.11"><div style="display:none">import { <a href="api/platform-browser-dynamic/platformBrowserDynamic" class="code-anchor">platformBrowserDynamic</a> } from '@angular/platform-browser-dynamic'; import { AppModule } from './app/app.module'; <a href="api/platform-browser-dynamic/platformBrowserDynamic" class="code-anchor">platformBrowserDynamic</a>().bootstrapModule(AppModule) .then(success =&gt; console.log(`Bootstrap success`)) .catch(err =&gt; console.error(err));</div><header class="ng-star-inserted">main.ts</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from main.ts">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="kwd">import</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><a href="api/platform-browser-dynamic/platformBrowserDynamic" class="code-anchor"><span class="pln">platformBrowserDynamic</span></a><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">from</span><span class="pln"> </span><span class="str">'@angular/platform-browser-dynamic'</span><span class="pun">;</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="typ">AppModule</span><span class="pln"> </span><span class="pun">}</span><span class="pln">              </span><span class="kwd">from</span><span class="pln"> </span><span class="str">'./app/app.module'</span><span class="pun">;</span><span class="pln">

</span><a href="api/platform-browser-dynamic/platformBrowserDynamic" class="code-anchor"><span class="pln">platformBrowserDynamic</span></a><span class="pun">().</span><span class="pln">bootstrapModule</span><span class="pun">(</span><span class="typ">AppModule</span><span class="pun">)</span><span class="pln">
  </span><span class="pun">.</span><span class="kwd">then</span><span class="pun">(</span><span class="pln">success </span><span class="pun">=&gt;</span><span class="pln"> console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">`Bootstrap success`</span><span class="pun">))</span><span class="pln">
  </span><span class="pun">.</span><span class="kwd">catch</span><span class="pun">(</span><span class="pln">err </span><span class="pun">=&gt;</span><span class="pln"> console</span><span class="pun">.</span><span class="pln">error</span><span class="pun">(</span><span class="pln">err</span><span class="pun">));</span></code>
    </pre></aio-code></code-example><p translation-result="on"><a href="guide/styleguide#toc">回到顶部</a></p><p translation-origin="off"><a href="guide/styleguide#toc">Back to top</a></p><a id="05-02"></a><h3 id="component-selectors" translation-result="on">组件选择器<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#component-selectors"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="component-selectors">Component selectors<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#component-selectors"><i class="material-icons">link</i></a></h3><h4 id="style-05-02" translation-result="on">风格 05-02<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-05-02"><i class="material-icons">link</i></a></h4><h4 translation-origin="off" id="style-05-02">Style 05-02<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-05-02"><i class="material-icons">link</i></a></h4><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>使用<em>中线命名法（dashed-case）</em>或叫<em>烤串命名法（kebab-case）</em>来命名组件的元素选择器。</p><p translation-origin="off"><strong>Do</strong> use <em>dashed-case</em> or <em>kebab-case</em> for naming the element selectors of components.</p></div><div class="s-why-last"><p translation-result="on"><strong>为何？</strong>让元素名和<a href="https://www.w3.org/TR/custom-elements/">自定义元素</a>规范保持一致。</p><p translation-origin="off"><strong>Why?</strong> Keeps the element names consistent with the specification for <a href="https://www.w3.org/TR/custom-elements/">Custom Elements</a>.</p></div><code-example path="styleguide/src/05-02/app/heroes/shared/hero-button/hero-button.component.avoid.ts" region="example" header="app/heroes/shared/hero-button/hero-button.component.ts" ng-version="9.0.0-rc.11" class="avoidFile"><div style="display:none">/* avoid */ @<a href="api/core/Component" class="code-anchor">Component</a>({ selector: 'tohHeroButton', <a href="api/core/Component#templateUrl" class="code-anchor">templateUrl</a>: './hero-button.component.html' }) export class HeroButtonComponent {}</div><header class="ng-star-inserted">app/heroes/shared/hero-button/hero-button.component.ts</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from app/heroes/shared/hero-button/hero-button.component.ts">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="com">/* avoid */</span><span class="pln">

</span><span class="lit">@</span><a href="api/core/Component" class="code-anchor"><span class="lit">Component</span></a><span class="pun">({</span><span class="pln">
  selector</span><span class="pun">:</span><span class="pln"> </span><span class="str">'tohHeroButton'</span><span class="pun">,</span><span class="pln">
  </span><a href="api/core/Component#templateUrl" class="code-anchor"><span class="pln">templateUrl</span></a><span class="pun">:</span><span class="pln"> </span><span class="str">'./hero-button.component.html'</span><span class="pln">
</span><span class="pun">})</span><span class="pln">
</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">HeroButtonComponent</span><span class="pln"> </span><span class="pun">{}</span></code>
    </pre></aio-code></code-example><code-tabs ng-version="9.0.0-rc.11"><div style="display:none"><code-pane header="app/heroes/shared/hero-button/hero-button.component.ts" path="styleguide/src/05-02/app/heroes/shared/hero-button/hero-button.component.ts" region="example">@<a href="api/core/Component" class="code-anchor">Component</a>({ selector: 'toh-hero-button', <a href="api/core/Component#templateUrl" class="code-anchor">templateUrl</a>: './hero-button.component.html' }) export class HeroButtonComponent {}</code-pane><code-pane header="app/app.component.html" path="styleguide/src/05-02/app/app.component.html">&lt;toh-hero-button&gt;&lt;/toh-hero-button&gt;</code-pane></div><mat-card class="mat-card"><mat-tab-group class="mat-tab-group code-tab-group mat-primary"><mat-tab-header class="mat-tab-header"><div aria-hidden="true" mat-ripple="" class="mat-ripple mat-tab-header-pagination mat-tab-header-pagination-before mat-elevation-z4 mat-tab-header-pagination-disabled"><div class="mat-tab-header-pagination-chevron"></div></div><div class="mat-tab-label-container"><div role="tablist" class="mat-tab-list" style="transform:translateX(0)"><div class="mat-tab-labels"><div role="tab" mattablabelwrapper="" mat-ripple="" cdkmonitorelementfocus="" class="mat-ripple mat-tab-label mat-tab-label-active ng-star-inserted" id="mat-tab-label-1-0" tabindex="0" aria-posinset="1" aria-setsize="2" aria-controls="mat-tab-content-1-0" aria-selected="true" aria-disabled="false"><div class="mat-tab-label-content"><span class="ng-star-inserted">app/heroes/shared/hero-button/hero-button.component.ts</span></div></div><div role="tab" mattablabelwrapper="" mat-ripple="" cdkmonitorelementfocus="" class="mat-ripple mat-tab-label ng-star-inserted" id="mat-tab-label-1-1" tabindex="-1" aria-posinset="2" aria-setsize="2" aria-controls="mat-tab-content-1-1" aria-selected="false" aria-disabled="false"><div class="mat-tab-label-content"><span class="ng-star-inserted">app/app.component.html</span></div></div></div><mat-ink-bar class="mat-ink-bar" style="visibility:visible;left:0;width:404px"></mat-ink-bar></div></div><div aria-hidden="true" mat-ripple="" class="mat-ripple mat-tab-header-pagination mat-tab-header-pagination-after mat-elevation-z4 mat-tab-header-pagination-disabled"><div class="mat-tab-header-pagination-chevron"></div></div></mat-tab-header><div class="mat-tab-body-wrapper"><mat-tab-body role="tabpanel" class="mat-tab-body ng-tns-c50-9 mat-tab-body-active ng-star-inserted" id="mat-tab-content-1-0" aria-labelledby="mat-tab-label-1-0"><div class="mat-tab-body-content ng-tns-c50-9 ng-trigger ng-trigger-translateTab" style="transform:none"><aio-code class="ng-star-inserted" style=""><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from app/heroes/shared/hero-button/hero-button.component.ts">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="lit">@</span><a href="api/core/Component" class="code-anchor"><span class="lit">Component</span></a><span class="pun">({</span><span class="pln">
  selector</span><span class="pun">:</span><span class="pln"> </span><span class="str">'toh-hero-button'</span><span class="pun">,</span><span class="pln">
  </span><a href="api/core/Component#templateUrl" class="code-anchor"><span class="pln">templateUrl</span></a><span class="pun">:</span><span class="pln"> </span><span class="str">'./hero-button.component.html'</span><span class="pln">
</span><span class="pun">})</span><span class="pln">
</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">HeroButtonComponent</span><span class="pln"> </span><span class="pun">{}</span></code>
    </pre></aio-code></div></mat-tab-body><mat-tab-body role="tabpanel" class="mat-tab-body ng-tns-c50-10 ng-star-inserted" id="mat-tab-content-1-1" aria-labelledby="mat-tab-label-1-1"><div class="mat-tab-body-content ng-tns-c50-10 ng-trigger ng-trigger-translateTab" style="transform:translate3d(100%,0,0);min-height:1px"></div></mat-tab-body></div></mat-tab-group></mat-card></code-tabs><p translation-result="on"><a href="guide/styleguide#toc">回到顶部</a></p><p translation-origin="off"><a href="guide/styleguide#toc">Back to top</a></p><a id="02-07"></a><h3 id="component-custom-prefix" translation-result="on">为组件添加自定义前缀<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#component-custom-prefix"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="component-custom-prefix">Component custom prefix<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#component-custom-prefix"><i class="material-icons">link</i></a></h3><h4 id="style-02-07" translation-result="on">风格 02-07<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-02-07"><i class="material-icons">link</i></a></h4><h4 translation-origin="off" id="style-02-07">Style 02-07<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-02-07"><i class="material-icons">link</i></a></h4><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>使用带连字符的小写元素选择器值（例如 <code>admin-users</code>）。</p><p translation-origin="off"><strong>Do</strong> use a hyphenated, lowercase element selector value; for example, <code>admin-users</code>.</p></div><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>为组件选择器添加自定义前缀。 例如，<code>toh</code> 前缀表示 <strong>T</strong>our <strong>o</strong>f <strong>H</strong>eroes（英雄指南），而前缀 `admin 表示管理特性区。</p><p translation-origin="off"><strong>Do</strong> use a custom prefix for a component selector. For example, the prefix <code>toh</code> represents <strong>T</strong>our <strong>o</strong>f <strong>H</strong>eroes and the prefix <code>admin</code> represents an admin feature area.</p></div><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>使用前缀来识别特性区或者应用程序本身。</p><p translation-origin="off"><strong>Do</strong> use a prefix that identifies the feature area or the app itself.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong>防止与其它应用中的组件和原生 HTML 元素发生命名冲突。</p><p translation-origin="off"><strong>Why?</strong> Prevents element name collisions with components in other apps and with native HTML elements.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong>更容易在其它应用中推广和共享组件。</p><p translation-origin="off"><strong>Why?</strong> Makes it easier to promote and share the component in other apps.</p></div><div class="s-why-last"><p translation-result="on"><strong>为何？</strong>组件在 DOM 中更容易被区分出来。</p><p translation-origin="off"><strong>Why?</strong> Components are easy to identify in the DOM.</p></div><code-example path="styleguide/src/02-07/app/heroes/hero.component.avoid.ts" region="example" header="app/heroes/hero.component.ts" ng-version="9.0.0-rc.11" class="avoidFile"><div style="display:none">/* avoid */ // HeroComponent is in the Tour of Heroes feature @<a href="api/core/Component" class="code-anchor">Component</a>({ selector: 'hero' }) export class HeroComponent {}</div><header class="ng-star-inserted">app/heroes/hero.component.ts</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from app/heroes/hero.component.ts">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="com">/* avoid */</span><span class="pln">

</span><span class="com">// HeroComponent is in the Tour of Heroes feature</span><span class="pln">
</span><span class="lit">@</span><a href="api/core/Component" class="code-anchor"><span class="lit">Component</span></a><span class="pun">({</span><span class="pln">
  selector</span><span class="pun">:</span><span class="pln"> </span><span class="str">'hero'</span><span class="pln">
</span><span class="pun">})</span><span class="pln">
</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">HeroComponent</span><span class="pln"> </span><span class="pun">{}</span></code>
    </pre></aio-code></code-example><code-example path="styleguide/src/02-07/app/users/users.component.avoid.ts" region="example" header="app/users/users.component.ts" ng-version="9.0.0-rc.11" class="avoidFile"><div style="display:none">/* avoid */ // UsersComponent is in an Admin feature @<a href="api/core/Component" class="code-anchor">Component</a>({ selector: 'users' }) export class UsersComponent {}</div><header class="ng-star-inserted">app/users/users.component.ts</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from app/users/users.component.ts">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="com">/* avoid */</span><span class="pln">

</span><span class="com">// UsersComponent is in an Admin feature</span><span class="pln">
</span><span class="lit">@</span><a href="api/core/Component" class="code-anchor"><span class="lit">Component</span></a><span class="pun">({</span><span class="pln">
  selector</span><span class="pun">:</span><span class="pln"> </span><span class="str">'users'</span><span class="pln">
</span><span class="pun">})</span><span class="pln">
</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">UsersComponent</span><span class="pln"> </span><span class="pun">{}</span></code>
    </pre></aio-code></code-example><code-example path="styleguide/src/02-07/app/heroes/hero.component.ts" region="example" header="app/heroes/hero.component.ts" ng-version="9.0.0-rc.11"><div style="display:none">@<a href="api/core/Component" class="code-anchor">Component</a>({ selector: 'toh-hero' }) export class HeroComponent {}</div><header class="ng-star-inserted">app/heroes/hero.component.ts</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from app/heroes/hero.component.ts">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="lit">@</span><a href="api/core/Component" class="code-anchor"><span class="lit">Component</span></a><span class="pun">({</span><span class="pln">
  selector</span><span class="pun">:</span><span class="pln"> </span><span class="str">'toh-hero'</span><span class="pln">
</span><span class="pun">})</span><span class="pln">
</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">HeroComponent</span><span class="pln"> </span><span class="pun">{}</span></code>
    </pre></aio-code></code-example><code-example path="styleguide/src/02-07/app/users/users.component.ts" region="example" header="app/users/users.component.ts" ng-version="9.0.0-rc.11"><div style="display:none">@<a href="api/core/Component" class="code-anchor">Component</a>({ selector: 'admin-users' }) export class UsersComponent {}</div><header class="ng-star-inserted">app/users/users.component.ts</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from app/users/users.component.ts">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="lit">@</span><a href="api/core/Component" class="code-anchor"><span class="lit">Component</span></a><span class="pun">({</span><span class="pln">
  selector</span><span class="pun">:</span><span class="pln"> </span><span class="str">'admin-users'</span><span class="pln">
</span><span class="pun">})</span><span class="pln">
</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">UsersComponent</span><span class="pln"> </span><span class="pun">{}</span></code>
    </pre></aio-code></code-example><p translation-result="on"><a href="guide/styleguide#toc">回到顶部</a></p><p translation-origin="off"><a href="guide/styleguide#toc">Back to top</a></p><a id="02-06"></a><h3 id="directive-selectors" translation-result="on">指令选择器<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#directive-selectors"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="directive-selectors">Directive selectors<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#directive-selectors"><i class="material-icons">link</i></a></h3><h4 id="style-02-06" translation-result="on">风格 02-06<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-02-06"><i class="material-icons">link</i></a></h4><h4 translation-origin="off" id="style-02-06">Style 02-06<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-02-06"><i class="material-icons">link</i></a></h4><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>使用小驼峰形式命名指令的选择器。</p><p translation-origin="off"><strong>Do</strong> Use lower camel case for naming the selectors of directives.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong>可以让指令中的属性名与视图中绑定的属性名保持一致。</p><p translation-origin="off"><strong>Why?</strong> Keeps the names of the properties defined in the directives that are bound to the view consistent with the attribute names.</p></div><div class="s-why-last"><p translation-result="on"><strong>为何？</strong> Angular 的 HTML 解析器是大小写敏感的，可以识别小驼峰形式。</p><p translation-origin="off"><strong>Why?</strong> The Angular HTML parser is case sensitive and recognizes lower camel case.</p></div><p translation-result="on"><a href="guide/styleguide#toc">回到顶部</a></p><p translation-origin="off"><a href="guide/styleguide#toc">Back to top</a></p><a id="02-08"></a><h3 id="directive-custom-prefix" translation-result="on">为指令添加自定义前缀<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#directive-custom-prefix"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="directive-custom-prefix">Directive custom prefix<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#directive-custom-prefix"><i class="material-icons">link</i></a></h3><h4 id="style-02-08" translation-result="on">风格 02-08<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-02-08"><i class="material-icons">link</i></a></h4><h4 translation-origin="off" id="style-02-08">Style 02-08<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-02-08"><i class="material-icons">link</i></a></h4><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>为指令的选择器添加自定义前缀（例如前缀 <code>toh</code> 来自 <strong>T</strong>our <strong>o</strong>f <strong>H</strong>eroes）。</p><p translation-origin="off"><strong>Do</strong> use a custom prefix for the selector of directives (e.g, the prefix <code>toh</code> from <strong>T</strong>our <strong>o</strong>f <strong>H</strong>eroes).</p></div><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>用小驼峰形式拼写非元素选择器，除非该选择器用于匹配原生 HTML 属性。</p><p translation-origin="off"><strong>Do</strong> spell non-element selectors in lower camel case unless the selector is meant to match a native HTML attribute.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong>防止名字冲突。</p><p translation-origin="off"><strong>Why?</strong> Prevents name collisions.</p></div><div class="s-why-last"><p translation-result="on"><strong>为何？</strong>指令更加容易被识别。</p><p translation-origin="off"><strong>Why?</strong> Directives are easily identified.</p></div><code-example path="styleguide/src/02-08/app/shared/validate.directive.avoid.ts" region="example" header="app/shared/validate.directive.ts" ng-version="9.0.0-rc.11" class="avoidFile"><div style="display:none">/* avoid */ @<a href="api/core/Directive" class="code-anchor">Directive</a>({ selector: '[validate]' }) export class ValidateDirective {}</div><header class="ng-star-inserted">app/shared/validate.directive.ts</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from app/shared/validate.directive.ts">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="com">/* avoid */</span><span class="pln">

</span><span class="lit">@</span><a href="api/core/Directive" class="code-anchor"><span class="lit">Directive</span></a><span class="pun">({</span><span class="pln">
  selector</span><span class="pun">:</span><span class="pln"> </span><span class="str">'[validate]'</span><span class="pln">
</span><span class="pun">})</span><span class="pln">
</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">ValidateDirective</span><span class="pln"> </span><span class="pun">{}</span></code>
    </pre></aio-code></code-example><code-example path="styleguide/src/02-08/app/shared/validate.directive.ts" region="example" header="app/shared/validate.directive.ts" ng-version="9.0.0-rc.11"><div style="display:none">@<a href="api/core/Directive" class="code-anchor">Directive</a>({ selector: '[tohValidate]' }) export class ValidateDirective {}</div><header class="ng-star-inserted">app/shared/validate.directive.ts</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from app/shared/validate.directive.ts">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="lit">@</span><a href="api/core/Directive" class="code-anchor"><span class="lit">Directive</span></a><span class="pun">({</span><span class="pln">
  selector</span><span class="pun">:</span><span class="pln"> </span><span class="str">'[tohValidate]'</span><span class="pln">
</span><span class="pun">})</span><span class="pln">
</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">ValidateDirective</span><span class="pln"> </span><span class="pun">{}</span></code>
    </pre></aio-code></code-example><p translation-result="on"><a href="guide/styleguide#toc">回到顶部</a></p><p translation-origin="off"><a href="guide/styleguide#toc">Back to top</a></p><a id="02-09"></a><h3 id="pipe-names" translation-result="on">管道名<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#pipe-names"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="pipe-names">Pipe names<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#pipe-names"><i class="material-icons">link</i></a></h3><h4 id="style-02-09" translation-result="on">风格 02-09<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-02-09"><i class="material-icons">link</i></a></h4><h4 translation-origin="off" id="style-02-09">Style 02-09<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-02-09"><i class="material-icons">link</i></a></h4><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>为所有管道使用一致的命名约定，用它们的特性来命名。 管道类名应该使用 <a href="guide/glossary#case-types">UpperCamelCase</a>（类名的通用约定），而相应的 <code>name</code> 字符串应该使用 <em>lowerCamelCase</em>。 <code>name</code> 字符串中不应该使用中线（“中线格式”或“烤串格式”）。</p><p translation-origin="off"><strong>Do</strong> use consistent names for all pipes, named after their feature. The pipe class name should use <a href="guide/glossary#case-types">UpperCamelCase</a> (the general convention for class names), and the corresponding <code>name</code> string should use <em>lowerCamelCase</em>. The <code>name</code> string cannot use hyphens ("dash-case" or "kebab-case").</p></div><div class="s-why-last"><p translation-result="on"><strong>为何？</strong>提供一致的方式快速识别和引用管道。</p><p translation-origin="off"><strong>Why?</strong> Provides a consistent way to quickly identify and reference pipes.</p></div><table width="100%"><colgroup><col width="50%"><col width="50%"></colgroup><tbody><tr><th><p translation-result="on">符号名</p><p translation-origin="off">Symbol Name</p></th><th><p translation-result="on">文件名</p><p translation-origin="off">File Name</p></th></tr><tr style=""><td><code-example hidecopy="" class="no-box" ng-version="9.0.0-rc.11"><div style="display:none">@<a href="api/core/Pipe" class="code-anchor">Pipe</a>({ name: 'ellipsis' }) export class EllipsisPipe implements <a href="api/core/PipeTransform" class="code-anchor">PipeTransform</a> { }</div><aio-code><pre class="lang- prettyprint">      
      <code class="animated fadeIn"><span class="lit">@</span><a href="api/core/Pipe" class="code-anchor"><span class="lit">Pipe</span></a><span class="pun">({</span><span class="pln"> name</span><span class="pun">:</span><span class="pln"> </span><span class="str">'ellipsis'</span><span class="pln"> </span><span class="pun">})</span><span class="pln">
</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">EllipsisPipe</span><span class="pln"> </span><span class="kwd">implements</span><span class="pln"> </span><a href="api/core/PipeTransform" class="code-anchor"><span class="typ">PipeTransform</span></a><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="pun">}</span></code>
    </pre></aio-code></code-example></td><td><p>ellipsis.pipe.ts</p></td></tr><tr style=""><td><code-example hidecopy="" class="no-box" ng-version="9.0.0-rc.11"><div style="display:none">@<a href="api/core/Pipe" class="code-anchor">Pipe</a>({ name: 'initCaps' }) export class InitCapsPipe implements <a href="api/core/PipeTransform" class="code-anchor">PipeTransform</a> { }</div><aio-code><pre class="lang- prettyprint">      
      <code class="animated fadeIn"><span class="lit">@</span><a href="api/core/Pipe" class="code-anchor"><span class="lit">Pipe</span></a><span class="pun">({</span><span class="pln"> name</span><span class="pun">:</span><span class="pln"> </span><span class="str">'initCaps'</span><span class="pln"> </span><span class="pun">})</span><span class="pln">
</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">InitCapsPipe</span><span class="pln"> </span><span class="kwd">implements</span><span class="pln"> </span><a href="api/core/PipeTransform" class="code-anchor"><span class="typ">PipeTransform</span></a><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="pun">}</span></code>
    </pre></aio-code></code-example></td><td><p>init-caps.pipe.ts</p></td></tr></tbody></table><p translation-result="on"><a href="guide/styleguide#toc">回到顶部</a></p><p translation-origin="off"><a href="guide/styleguide#toc">Back to top</a></p><a id="02-10"></a><h3 id="unit-test-file-names" translation-result="on">单元测试文件名<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#unit-test-file-names"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="unit-test-file-names">Unit test file names<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#unit-test-file-names"><i class="material-icons">link</i></a></h3><h4 id="style-02-10" translation-result="on">风格 02-10<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-02-10"><i class="material-icons">link</i></a></h4><h4 translation-origin="off" id="style-02-10">Style 02-10<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-02-10"><i class="material-icons">link</i></a></h4><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>测试规格文件名与被测试组件文件名相同。</p><p translation-origin="off"><strong>Do</strong> name test specification files the same as the component they test.</p></div><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>测试规格文件名添加 <code>.spec</code> 后缀。</p><p translation-origin="off"><strong>Do</strong> name test specification files with a suffix of <code>.spec</code>.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong>提供一致的方式来快速识别测试。</p><p translation-origin="off"><strong>Why?</strong> Provides a consistent way to quickly identify tests.</p></div><div class="s-why-last"><p translation-result="on"><strong>为何？</strong>提供一个与 <a href="http://karma-runner.github.io/">karma</a> 或者其它测试运行器相配的命名模式。</p><p translation-origin="off"><strong>Why?</strong> Provides pattern matching for <a href="http://karma-runner.github.io/">karma</a> or other test runners.</p></div><table width="100%"><colgroup><col width="50%"><col width="50%"></colgroup><tbody><tr><th><p translation-result="on">测试类型</p><p translation-origin="off">Test Type</p></th><th><p translation-result="on">文件名</p><p translation-origin="off">File Names</p></th></tr><tr style=""><td><p translation-result="on">组件</p><p translation-origin="off">Components</p></td><td><p>heroes.component.spec.ts</p><p>hero-list.component.spec.ts</p><p>hero-detail.component.spec.ts</p></td></tr><tr style=""><td><p translation-result="on">服务</p><p translation-origin="off">Services</p></td><td><p>logger.service.spec.ts</p><p>hero.service.spec.ts</p><p>filter-text.service.spec.ts</p></td></tr><tr style=""><td><p translation-result="on">管道</p><p translation-origin="off">Pipes</p></td><td><p>ellipsis.pipe.spec.ts</p><p>init-caps.pipe.spec.ts</p></td></tr></tbody></table><p translation-result="on"><a href="guide/styleguide#toc">回到顶部</a></p><p translation-origin="off"><a href="guide/styleguide#toc">Back to top</a></p><a id="02-11"></a><h3 id="end-to-end-e2e-test-file-names" translation-result="on"><em>端到端</em>（E2E）测试的文件名<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#end-to-end-e2e-test-file-names"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="end-to-end-e2e-test-file-names"><em>End-to-End</em> (E2E) test file names<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#end-to-end-e2e-test-file-names"><i class="material-icons">link</i></a></h3><h4 id="style-02-11" translation-result="on">风格 02-11<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-02-11"><i class="material-icons">link</i></a></h4><h4 translation-origin="off" id="style-02-11">Style 02-11<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-02-11"><i class="material-icons">link</i></a></h4><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>端到端测试规格文件和它们所测试的特性同名，添加 <code>.e2e-spec</code> 后缀。</p><p translation-origin="off"><strong>Do</strong> name end-to-end test specification files after the feature they test with a suffix of <code>.e2e-spec</code>.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong>提供一致的方式快速识别端到端测试文件。</p><p translation-origin="off"><strong>Why?</strong> Provides a consistent way to quickly identify end-to-end tests.</p></div><div class="s-why-last"><p translation-result="on"><strong>为何？</strong>提供一个与测试运行器和构建自动化匹配的模式。</p><p translation-origin="off"><strong>Why?</strong> Provides pattern matching for test runners and build automation.</p></div><table width="100%"><colgroup><col width="50%"><col width="50%"></colgroup><tbody><tr><th><p translation-result="on">测试类型</p><p translation-origin="off">Test Type</p></th><th><p translation-result="on">文件名</p><p translation-origin="off">File Names</p></th></tr><tr style=""><td><p translation-result="on">端到端测试</p><p translation-origin="off">End-to-End Tests</p></td><td><p>app.e2e-spec.ts</p><p>heroes.e2e-spec.ts</p></td></tr></tbody></table><p translation-result="on"><a href="guide/styleguide#toc">回到顶部</a></p><p translation-origin="off"><a href="guide/styleguide#toc">Back to top</a></p><a id="02-12"></a><h3 id="angular-ngmodule-names" translation-result="on">Angular <em>NgModule</em> 命名<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#angular-ngmodule-names"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="angular-ngmodule-names">Angular <em>NgModule</em> names<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#angular-ngmodule-names"><i class="material-icons">link</i></a></h3><h4 id="style-02-12" translation-result="on">风格 02-12<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-02-12"><i class="material-icons">link</i></a></h4><h4 translation-origin="off" id="style-02-12">Style 02-12<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-02-12"><i class="material-icons">link</i></a></h4><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>为符号名添加 <code>Module</code> 后缀</p><p translation-origin="off"><strong>Do</strong> append the symbol name with the suffix <code>Module</code>.</p></div><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>为文件名添加 <code>.module.ts</code> 扩展名。</p><p translation-origin="off"><strong>Do</strong> give the file name the <code>.module.ts</code> extension.</p></div><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>用特性名和所在目录命名模块。</p><p translation-origin="off"><strong>Do</strong> name the module after the feature and folder it resides in.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong>提供一致的方式来快速标识和引用模块。</p><p translation-origin="off"><strong>Why?</strong> Provides a consistent way to quickly identify and reference modules.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong>大驼峰命名法是一种命名约定，用来标识可用构造函数实例化的对象。</p><p translation-origin="off"><strong>Why?</strong> Upper camel case is conventional for identifying objects that can be instantiated using a constructor.</p></div><div class="s-why-last"><p translation-result="on"><strong>为何？</strong>很容易就能看出这个模块是同名特性的根模块。</p><p translation-origin="off"><strong>Why?</strong> Easily identifies the module as the root of the same named feature.</p></div><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>为 <em>RoutingModule</em> 类名添加 <code>RoutingModule</code> 后缀。</p><p translation-origin="off"><strong>Do</strong> suffix a <em>RoutingModule</em> class name with <code>RoutingModule</code>.</p></div><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>为 <em>RoutingModule</em> 的文件名添加 <code>-routing.module.ts</code> 后缀。</p><p translation-origin="off"><strong>Do</strong> end the filename of a <em>RoutingModule</em> with <code>-routing.module.ts</code>.</p></div><div class="s-why-last"><p translation-result="on"><strong>为何？</strong><code>RoutingModule</code> 是一种专门用来配置 Angular 路由器的模块。 “类名和文件名保持一致”的约定使这些模块易于发现和验证。</p><p translation-origin="off"><strong>Why?</strong> A <code>RoutingModule</code> is a module dedicated exclusively to configuring the Angular router. A consistent class and file name convention make these modules easy to spot and verify.</p></div><table width="100%"><colgroup><col width="50%"><col width="50%"></colgroup><tbody><tr><th><p translation-result="on">符号名</p><p translation-origin="off">Symbol Name</p></th><th><p translation-result="on">文件名</p><p translation-origin="off">File Name</p></th></tr><tr style=""><td><code-example hidecopy="" class="no-box" ng-version="9.0.0-rc.11"><div style="display:none">@<a href="api/core/NgModule" class="code-anchor">NgModule</a>({ ... }) export class AppModule { }</div><aio-code><pre class="lang- prettyprint">      
      <code class="animated fadeIn"><span class="lit">@</span><a href="api/core/NgModule" class="code-anchor"><span class="lit">NgModule</span></a><span class="pun">({</span><span class="pln"> </span><span class="pun">...</span><span class="pln"> </span><span class="pun">})</span><span class="pln">
</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">AppModule</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="pun">}</span></code>
    </pre></aio-code></code-example></td><td><p>app.module.ts</p></td></tr><tr style=""><td><code-example hidecopy="" class="no-box" ng-version="9.0.0-rc.11"><div style="display:none">@<a href="api/core/NgModule" class="code-anchor">NgModule</a>({ ... }) export class HeroesModule { }</div><aio-code><pre class="lang- prettyprint">      
      <code class="animated fadeIn"><span class="lit">@</span><a href="api/core/NgModule" class="code-anchor"><span class="lit">NgModule</span></a><span class="pun">({</span><span class="pln"> </span><span class="pun">...</span><span class="pln"> </span><span class="pun">})</span><span class="pln">
</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">HeroesModule</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="pun">}</span></code>
    </pre></aio-code></code-example></td><td><p>heroes.module.ts</p></td></tr><tr style=""><td><code-example hidecopy="" class="no-box" ng-version="9.0.0-rc.11"><div style="display:none">@<a href="api/core/NgModule" class="code-anchor">NgModule</a>({ ... }) export class VillainsModule { }</div><aio-code><pre class="lang- prettyprint">      
      <code class="animated fadeIn"><span class="lit">@</span><a href="api/core/NgModule" class="code-anchor"><span class="lit">NgModule</span></a><span class="pun">({</span><span class="pln"> </span><span class="pun">...</span><span class="pln"> </span><span class="pun">})</span><span class="pln">
</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">VillainsModule</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="pun">}</span></code>
    </pre></aio-code></code-example></td><td><p>villains.module.ts</p></td></tr><tr style=""><td><code-example hidecopy="" class="no-box" ng-version="9.0.0-rc.11"><div style="display:none">@<a href="api/core/NgModule" class="code-anchor">NgModule</a>({ ... }) export class AppRoutingModule { }</div><aio-code><pre class="lang- prettyprint">      
      <code class="animated fadeIn"><span class="lit">@</span><a href="api/core/NgModule" class="code-anchor"><span class="lit">NgModule</span></a><span class="pun">({</span><span class="pln"> </span><span class="pun">...</span><span class="pln"> </span><span class="pun">})</span><span class="pln">
</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">AppRoutingModule</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="pun">}</span></code>
    </pre></aio-code></code-example></td><td><p>app-routing.module.ts</p></td></tr><tr style=""><td><code-example hidecopy="" class="no-box" ng-version="9.0.0-rc.11"><div style="display:none">@<a href="api/core/NgModule" class="code-anchor">NgModule</a>({ ... }) export class HeroesRoutingModule { }</div><aio-code><pre class="lang- prettyprint">      
      <code class="animated fadeIn"><span class="lit">@</span><a href="api/core/NgModule" class="code-anchor"><span class="lit">NgModule</span></a><span class="pun">({</span><span class="pln"> </span><span class="pun">...</span><span class="pln"> </span><span class="pun">})</span><span class="pln">
</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">HeroesRoutingModule</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="pun">}</span></code>
    </pre></aio-code></code-example></td><td><p>heroes-routing.module.ts</p></td></tr></tbody></table><p translation-result="on"><a href="guide/styleguide#toc">回到顶部</a></p><p translation-origin="off"><a href="guide/styleguide#toc">Back to top</a></p><h2 id="application-structure-and-ngmodules" translation-result="on">应用程序结构与 NgModule<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#application-structure-and-ngmodules"><i class="material-icons">link</i></a></h2><h2 translation-origin="off" id="application-structure-and-ngmodules">Application structure and NgModules<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#application-structure-and-ngmodules"><i class="material-icons">link</i></a></h2><p translation-result="on">准备一个近期实施方案和一个长期的愿景。从零开始，但要考虑应用程序接下来的路往哪儿走。</p><p translation-origin="off">Have a near-term view of implementation and a long-term vision. Start small but keep in mind where the app is heading down the road.</p><p translation-result="on">所有应用程序的源代码都放到名叫 <code>src</code> 的目录里。 所有特性区都在自己的文件夹中，带有它们自己的 NgModule。</p><p translation-origin="off">All of the app's code goes in a folder named <code>src</code>. All feature areas are in their own folder, with their own NgModule.</p><p translation-result="on">所有内容都遵循每个文件一个特性的原则。每个组件、服务和管道都在自己的文件里。 所有第三方程序包保存到其它目录里，而不是 <code>src</code> 目录。 你不会修改它们，所以不希望它们弄乱你的应用程序。 使用本指南介绍的文件命名约定。</p><p translation-origin="off">All content is one asset per file. Each component, service, and pipe is in its own file. All third party vendor scripts are stored in another folder and not in the <code>src</code> folder. You didn't write them and you don't want them cluttering <code>src</code>. Use the naming conventions for files in this guide.</p><p translation-result="on"><a href="guide/styleguide#toc">回到顶部</a></p><p translation-origin="off"><a href="guide/styleguide#toc">Back to top</a></p><a id="04-01"></a><h3 id="lift"><em>LIFT</em><a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#lift"><i class="material-icons">link</i></a></h3><h4 id="style-04-01" translation-result="on">风格 04-01<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-04-01"><i class="material-icons">link</i></a></h4><h4 translation-origin="off" id="style-04-01">Style 04-01<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-04-01"><i class="material-icons">link</i></a></h4><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>组织应用的结构，力求：快速定位 (<code>L</code>ocate) 代码、一眼识别 (<code>I</code>dentify) 代码、 尽量保持扁平结构 (<code>F</code>lattest) 和尝试 (<code>T</code>ry) 遵循 DRY (Do Not Repeat Yourself, 不重复自己) 原则。</p><p translation-origin="off"><strong>Do</strong> structure the app such that you can <strong>L</strong>ocate code quickly, <strong>I</strong>dentify the code at a glance, keep the <strong>F</strong>lattest structure you can, and <strong>T</strong>ry to be DRY.</p></div><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>四项基本原则定义文件结构，上面的原则是按重要顺序排列的。</p><p translation-origin="off"><strong>Do</strong> define the structure to follow these four basic guidelines, listed in order of importance.</p></div><div class="s-why-last"><p translation-result="on"><strong>为何？</strong>LIFT 提供了一致的结构，它具有扩展性强、模块化的特性。因为容易快速锁定代码，提高了开发者的效率。 另外，检查应用结构是否合理的方法是问问自己：我能快速打开与此特性有关的所有文件并开始工作吗？</p><p translation-origin="off"><strong>Why?</strong> LIFT provides a consistent structure that scales well, is modular, and makes it easier to increase developer efficiency by finding code quickly. To confirm your intuition about a particular structure, ask: <em>can I quickly open and start work in all of the related files for this feature</em>?</p></div><p translation-result="on"><a href="guide/styleguide#toc">回到顶部</a></p><p translation-origin="off"><a href="guide/styleguide#toc">Back to top</a></p><a id="04-02"></a><h3 id="locate" translation-result="on">定位<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#locate"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="locate">Locate<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#locate"><i class="material-icons">link</i></a></h3><h4 id="style-04-02" translation-result="on">风格 04-02<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-04-02"><i class="material-icons">link</i></a></h4><h4 translation-origin="off" id="style-04-02">Style 04-02<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-04-02"><i class="material-icons">link</i></a></h4><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>直观、简单和快速地定位代码。</p><p translation-origin="off"><strong>Do</strong> make locating code intuitive, simple, and fast.</p></div><div class="s-why-last"><p translation-result="on"><strong>为何？</strong> 要想高效的工作，就必须能迅速找到文件，特别是当不知道（或不记得）文件<em>名</em>时。 把相关的文件一起放在一个直观的位置可以节省时间。 富有描述性的目录结构会让你和后面的维护者眼前一亮。</p><p translation-origin="off"><strong>Why?</strong> To work efficiently you must be able to find files quickly, especially when you do not know (or do not remember) the file <em>names</em>. Keeping related files near each other in an intuitive location saves time. A descriptive folder structure makes a world of difference to you and the people who come after you.</p></div><p translation-result="on"><a href="guide/styleguide#toc">回到顶部</a></p><p translation-origin="off"><a href="guide/styleguide#toc">Back to top</a></p><a id="04-03"></a><h3 id="identify" translation-result="on">识别<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#identify"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="identify">Identify<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#identify"><i class="material-icons">link</i></a></h3><h4 id="style-04-03" translation-result="on">风格 04-03<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-04-03"><i class="material-icons">link</i></a></h4><h4 translation-origin="off" id="style-04-03">Style 04-03<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-04-03"><i class="material-icons">link</i></a></h4><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>命名文件到这个程度：看到名字立刻知道它包含了什么，代表了什么。</p><p translation-origin="off"><strong>Do</strong> name the file such that you instantly know what it contains and represents.</p></div><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>文件名要具有说明性，确保文件中只包含一个组件。</p><p translation-origin="off"><strong>Do</strong> be descriptive with file names and keep the contents of the file to exactly one component.</p></div><div class="s-rule avoid"><p translation-result="on"><strong>避免</strong>创建包含多个组件、服务或者混合体的文件。</p><p translation-origin="off"><strong>Avoid</strong> files with multiple components, multiple services, or a mixture.</p></div><div class="s-why-last"><p translation-result="on"><strong>为何？</strong>花费更少的时间来查找和琢磨代码，就会变得更有效率。 较长的文件名远胜于<em>较短却容易混淆的</em>缩写名。</p><p translation-origin="off"><strong>Why?</strong> Spend less time hunting and pecking for code, and become more efficient. Longer file names are far better than <em>short-but-obscure</em> abbreviated names.</p></div><div class="alert is-helpful"><p translation-result="on">当你有一组小型、紧密相关的特性时，违反<em>一物一文件</em>的规则可能会更好， 这种情况下单一文件可能会比多个文件更容易发现和理解。注意这个例外。</p><p translation-origin="off">It may be advantageous to deviate from the <em>one-thing-per-file</em> rule when you have a set of small, closely-related features that are better discovered and understood in a single file than as multiple files. Be wary of this loophole.</p></div><p translation-result="on"><a href="guide/styleguide#toc">回到顶部</a></p><p translation-origin="off"><a href="guide/styleguide#toc">Back to top</a></p><a id="04-04"></a><h3 id="flat" translation-result="on">扁平<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#flat"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="flat">Flat<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#flat"><i class="material-icons">link</i></a></h3><h4 id="style-04-04" translation-result="on">风格 04-04<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-04-04"><i class="material-icons">link</i></a></h4><h4 translation-origin="off" id="style-04-04">Style 04-04<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-04-04"><i class="material-icons">link</i></a></h4><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>尽可能保持扁平的目录结构。</p><p translation-origin="off"><strong>Do</strong> keep a flat folder structure as long as possible.</p></div><div class="s-rule consider"><p translation-result="on"><strong>考虑</strong>当同一目录下达到 7 个或更多个文件时创建子目录。</p><p translation-origin="off"><strong>Consider</strong> creating sub-folders when a folder reaches seven or more files.</p></div><div class="s-rule consider"><p translation-result="on"><strong>考虑</strong>配置 IDE，以隐藏无关的文件，例如生成出来的 <code>.js</code> 文件和 <code>.js.map</code> 文件等。</p><p translation-origin="off"><strong>Consider</strong> configuring the IDE to hide distracting, irrelevant files such as generated <code>.js</code> and <code>.js.map</code> files.</p></div><div class="s-why-last"><p translation-result="on"><strong>为何？</strong>没人想要在超过七层的目录中查找文件。扁平的结构有利于搜索。</p><p translation-origin="off"><strong>Why?</strong> No one wants to search for a file through seven levels of folders. A flat structure is easy to scan.</p><p translation-result="on">另一方面，<a href="https://en.wikipedia.org/wiki/The_Magical_Number_Seven,_Plus_or_Minus_Two" target="_blank">心理学家们相信</a>， 当关注的事物超过 9 个时，人类就会开始感到吃力。 所以，当一个文件夹中的文件有 10 个或更多个文件时，可能就是创建子目录的时候了。</p><p translation-origin="off">On the other hand, <a href="https://en.wikipedia.org/wiki/The_Magical_Number_Seven,_Plus_or_Minus_Two">psychologists believe</a> that humans start to struggle when the number of adjacent interesting things exceeds nine. So when a folder has ten or more files, it may be time to create subfolders.</p><p translation-result="on">还是根据你自己的舒适度而定吧。 除非创建新文件夹能有显著的价值，否则尽量使用扁平结构。</p><p translation-origin="off">Base your decision on your comfort level. Use a flatter structure until there is an obvious value to creating a new folder.</p></div><p translation-result="on"><a href="guide/styleguide#toc">回到顶部</a></p><p translation-origin="off"><a href="guide/styleguide#toc">Back to top</a></p><a id="04-05"></a><h3 id="t-dry-try-to-be-dry" translation-result="on"><em>T-DRY</em>（尽量不重复自己）<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#t-dry-try-to-be-dry"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="t-dry-try-to-be-dry"><em>T-DRY</em> (Try to be <em>DRY</em>)<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#t-dry-try-to-be-dry"><i class="material-icons">link</i></a></h3><h4 id="style-04-05" translation-result="on">风格 04-05<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-04-05"><i class="material-icons">link</i></a></h4><h4 translation-origin="off" id="style-04-05">Style 04-05<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-04-05"><i class="material-icons">link</i></a></h4><div class="s-rule do"><p translation-result="on"><strong>坚持</strong> DRY（Don't Repeat Yourself，不重复自己）。</p><p translation-origin="off"><strong>Do</strong> be DRY (Don't Repeat Yourself).</p></div><div class="s-rule avoid"><p translation-result="on"><strong>避免</strong>过度 DRY，以致牺牲了阅读性。</p><p translation-origin="off"><strong>Avoid</strong> being so DRY that you sacrifice readability.</p></div><div class="s-why-last"><p translation-result="on"><strong>为何？</strong>虽然 DRY 很重要，但如果要以牺牲 LIFT 的其它原则为代价，那就不值得了。 这也就是为什么它被称为 <em>T-DRY</em>。 例如，把组件命名为 <code>hero-view.component.html</code> 是多余的，因为带有 <code>.html</code> 扩展名的文件显然就是一个视图 (view)。 但如果它不那么显著，或不符合常规，就把它写出来。</p><p translation-origin="off"><strong>Why?</strong> Being DRY is important, but not crucial if it sacrifices the other elements of LIFT. That's why it's called <em>T-DRY</em>. For example, it's redundant to name a template <code>hero-view.component.html</code> because with the <code>.html</code> extension, it is obviously a view. But if something is not obvious or departs from a convention, then spell it out.</p></div><p translation-result="on"><a href="guide/styleguide#toc">回到顶部</a></p><p translation-origin="off"><a href="guide/styleguide#toc">Back to top</a></p><a id="04-06"></a><h3 id="overall-structural-guidelines" translation-result="on">总体结构的指导原则<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#overall-structural-guidelines"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="overall-structural-guidelines">Overall structural guidelines<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#overall-structural-guidelines"><i class="material-icons">link</i></a></h3><h4 id="style-04-06" translation-result="on">风格 04-06<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-04-06"><i class="material-icons">link</i></a></h4><h4 translation-origin="off" id="style-04-06">Style 04-06<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-04-06"><i class="material-icons">link</i></a></h4><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>从零开始，但要考虑应用程序接下来的路往哪儿走。</p><p translation-origin="off"><strong>Do</strong> start small but keep in mind where the app is heading down the road.</p></div><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>有一个近期实施方案和一个长期的愿景。</p><p translation-origin="off"><strong>Do</strong> have a near term view of implementation and a long term vision.</p></div><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>把所有源代码都放到名为 <code>src</code> 的目录里。</p><p translation-origin="off"><strong>Do</strong> put all of the app's code in a folder named <code>src</code>.</p></div><div class="s-rule consider"><p translation-result="on"><strong>坚持</strong>如果组件具有多个伴生文件 (<code>.ts</code>、<code>.html</code>、<code>.css</code> 和 <code>.spec</code>)，就为它创建一个文件夹。</p><p translation-origin="off"><strong>Consider</strong> creating a folder for a component when it has multiple accompanying files (<code>.ts</code>, <code>.html</code>, <code>.css</code> and <code>.spec</code>).</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong> 在早期阶段能够帮助保持应用的结构小巧且易于维护，这样当应用增长时就容易进化了。</p><p translation-origin="off"><strong>Why?</strong> Helps keep the app structure small and easy to maintain in the early stages, while being easy to evolve as the app grows.</p></div><div class="s-why-last"><p translation-result="on"><strong>为何?</strong> 组件通常有四个文件 (<code>*.html</code>、 <code>*.css</code>、 <code>*.ts</code> 和 <code>*.spec.ts</code>)，它们很容易把一个目录弄乱。</p><p translation-origin="off"><strong>Why?</strong> Components often have four files (e.g. <code>*.html</code>, <code>*.css</code>, <code>*.ts</code>, and <code>*.spec.ts</code>) and can clutter a folder quickly.</p></div><a id="file-tree"></a><p translation-result="on">下面是符合规范的目录和文件结构</p><p translation-origin="off">Here is a compliant folder and file structure:</p><div class="filetree"><div class="file"><p>&lt;project root&gt;</p></div><div class="children"><div class="file"><p>src</p></div><div class="children"><div class="file"><p>app</p></div><div class="children"><div class="file"><p>core</p></div><div class="children"><div class="file"><p>exception.service.ts|spec.ts</p></div><div class="file"><p>user-profile.service.ts|spec.ts</p></div></div><div class="file"><p>heroes</p></div><div class="children"><div class="file"><p>hero</p></div><div class="children"><div class="file"><p>hero.component.ts|html|css|spec.ts</p></div></div><div class="file"><p>hero-list</p></div><div class="children"><div class="file"><p>hero-list.component.ts|html|css|spec.ts</p></div></div><div class="file"><p>shared</p></div><div class="children"><div class="file"><p>hero-button.component.ts|html|css|spec.ts</p></div><div class="file"><p>hero.model.ts</p></div><div class="file"><p>hero.service.ts|spec.ts</p></div></div><div class="file"><p>heroes.component.ts|html|css|spec.ts</p></div><div class="file"><p>heroes.module.ts</p></div><div class="file"><p>heroes-routing.module.ts</p></div></div><div class="file"><p>shared</p></div><div class="children"><div class="file"><p>shared.module.ts</p></div><div class="file"><p>init-caps.pipe.ts|spec.ts</p></div><div class="file"><p>filter-text.component.ts|spec.ts</p></div><div class="file"><p>filter-text.service.ts|spec.ts</p></div></div><div class="file"><p>villains</p></div><div class="children"><div class="file"><p>villain</p></div><div class="children"><div class="file"><p>...</p></div></div><div class="file"><p>villain-list</p></div><div class="children"><div class="file"><p>...</p></div></div><div class="file"><p>shared</p></div><div class="children"><div class="file"><p>...</p></div></div><div class="file"><p>villains.component.ts|html|css|spec.ts</p></div><div class="file"><p>villains.module.ts</p></div><div class="file"><p>villains-routing.module.ts</p></div></div><div class="file"><p>app.component.ts|html|css|spec.ts</p></div><div class="file"><p>app.module.ts</p></div><div class="file"><p>app-routing.module.ts</p></div></div><div class="file"><p>main.ts</p></div><div class="file"><p>index.html</p></div><div class="file"><p>...</p></div></div><div class="file"><p>node_modules/...</p></div><div class="file"><p>...</p></div></div></div><div class="alert is-helpful"><p translation-result="on">把组件放在专用目录中的方式广受欢迎，对于小型应用，还可以保持组件扁平化（而不是放在专用目录中）。 这样会把四个文件放在现有目录中，也会减少目录的嵌套。无论你如何选择，请保持一致。</p><p translation-origin="off">While components in dedicated folders are widely preferred, another option for small apps is to keep components flat (not in a dedicated folder). This adds up to four files to the existing folder, but also reduces the folder nesting. Whatever you choose, be consistent.</p></div><p translation-result="on"><a href="guide/styleguide#toc">回到顶部</a></p><p translation-origin="off"><a href="guide/styleguide#toc">Back to top</a></p><a id="04-07"></a><h3 id="folders-by-feature-structure" translation-result="on">按特性组织的目录结构<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#folders-by-feature-structure"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="folders-by-feature-structure"><em>Folders-by-feature</em> structure<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#folders-by-feature-structure"><i class="material-icons">link</i></a></h3><h4 id="style-04-07" translation-result="on">风格 04-07<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-04-07"><i class="material-icons">link</i></a></h4><h4 translation-origin="off" id="style-04-07">Style 04-07<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-04-07"><i class="material-icons">link</i></a></h4><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>根据特性区命名目录。</p><p translation-origin="off"><strong>Do</strong> create folders named for the feature area they represent.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong>开发人员可以快速定位代码，扫一眼就能知道每个文件代表什么，目录尽可能保持扁平，既没有重复也没有多余的名字。</p><p translation-origin="off"><strong>Why?</strong> A developer can locate the code and identify what each file represents at a glance. The structure is as flat as it can be and there are no repetitive or redundant names.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong> LIFT 原则中包含了所有这些。</p><p translation-origin="off"><strong>Why?</strong> The LIFT guidelines are all covered.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong>遵循 LIFT 原则精心组织内容，避免应用变得杂乱无章。</p><p translation-origin="off"><strong>Why?</strong> Helps reduce the app from becoming cluttered through organizing the content and keeping them aligned with the LIFT guidelines.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong>当有很多文件时（例如 10 个以上），在专用目录型结构中定位它们会比在扁平结构中更容易。</p><p translation-origin="off"><strong>Why?</strong> When there are a lot of files, for example 10+, locating them is easier with a consistent folder structure and more difficult in a flat structure.</p></div><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>为每个特性区创建一个 NgModule。</p><p translation-origin="off"><strong>Do</strong> create an NgModule for each feature area.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong> NgModule 使惰性加载可路由的特性变得更容易。</p><p translation-origin="off"><strong>Why?</strong> NgModules make it easy to lazy load routable features.</p></div><div class="s-why-last"><p translation-result="on"><strong>为何？</strong> NgModule 隔离、测试和复用特性更容易。</p><p translation-origin="off"><strong>Why?</strong> NgModules make it easier to isolate, test, and reuse features.</p></div><div><p translation-result="on">欲知详情，参见<a href="guide/styleguide#file-tree">目录和文件结构的范例</a></p><p translation-origin="off">For more information, refer to <a href="guide/styleguide#file-tree">this folder and file structure example.</a></p></div><p translation-result="on"><a href="guide/styleguide#toc">回到顶部</a></p><p translation-origin="off"><a href="guide/styleguide#toc">Back to top</a></p><a id="04-08"></a><h3 id="app-root-module" translation-result="on">应用的<em>根模块</em><a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#app-root-module"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="app-root-module">App <em>root module</em><a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#app-root-module"><i class="material-icons">link</i></a></h3><h4 id="style-04-08" translation-result="on">风格 04-08<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-04-08"><i class="material-icons">link</i></a></h4><h4 translation-origin="off" id="style-04-08">Style 04-08<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-04-08"><i class="material-icons">link</i></a></h4><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>在应用的根目录创建一个 NgModule（例如 <code>/src/app</code>）。</p><p translation-origin="off"><strong>Do</strong> create an NgModule in the app's root folder, for example, in <code>/src/app</code>.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong>每个应用都至少需要一个根 NgModule。</p><p translation-origin="off"><strong>Why?</strong> Every app requires at least one root NgModule.</p></div><div class="s-rule consider"><p translation-result="on"><strong>考虑</strong>把根模块命名为 <code>app.module.ts</code>。</p><p translation-origin="off"><strong>Consider</strong> naming the root module <code>app.module.ts</code>.</p></div><div class="s-why-last"><p translation-result="on"><strong>为何？</strong>能让定位和识别根模块变得更容易。</p><p translation-origin="off"><strong>Why?</strong> Makes it easier to locate and identify the root module.</p></div><code-example path="styleguide/src/04-08/app/app.module.ts" region="example" header="app/app.module.ts" ng-version="9.0.0-rc.11"><div style="display:none">import { <a href="api/core/NgModule" class="code-anchor">NgModule</a> } from '@angular/core'; import { <a href="api/platform-browser/BrowserModule" class="code-anchor">BrowserModule</a> } from '@angular/platform-browser'; import { AppComponent } from './app.component'; import { HeroesComponent } from './heroes/heroes.component'; @<a href="api/core/NgModule" class="code-anchor">NgModule</a>({ imports: [ <a href="api/platform-browser/BrowserModule" class="code-anchor">BrowserModule</a>, ], declarations: [ AppComponent, HeroesComponent ], exports: [ AppComponent ], entryComponents: [ AppComponent ] }) export class AppModule {}</div><header class="ng-star-inserted">app/app.module.ts</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from app/app.module.ts">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="kwd">import</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><a href="api/core/NgModule" class="code-anchor"><span class="typ">NgModule</span></a><span class="pln"> </span><span class="pun">}</span><span class="pln">      </span><span class="kwd">from</span><span class="pln"> </span><span class="str">'@angular/core'</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">import</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><a href="api/platform-browser/BrowserModule" class="code-anchor"><span class="typ">BrowserModule</span></a><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">from</span><span class="pln"> </span><span class="str">'@angular/platform-browser'</span><span class="pun">;</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="typ">AppComponent</span><span class="pln"> </span><span class="pun">}</span><span class="pln">    </span><span class="kwd">from</span><span class="pln"> </span><span class="str">'./app.component'</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">import</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="typ">HeroesComponent</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">from</span><span class="pln"> </span><span class="str">'./heroes/heroes.component'</span><span class="pun">;</span><span class="pln">

</span><span class="lit">@</span><a href="api/core/NgModule" class="code-anchor"><span class="lit">NgModule</span></a><span class="pun">({</span><span class="pln">
  imports</span><span class="pun">:</span><span class="pln"> </span><span class="pun">[</span><span class="pln">
    </span><a href="api/platform-browser/BrowserModule" class="code-anchor"><span class="typ">BrowserModule</span></a><span class="pun">,</span><span class="pln">
  </span><span class="pun">],</span><span class="pln">
  declarations</span><span class="pun">:</span><span class="pln"> </span><span class="pun">[</span><span class="pln">
    </span><span class="typ">AppComponent</span><span class="pun">,</span><span class="pln">
    </span><span class="typ">HeroesComponent</span><span class="pln">
  </span><span class="pun">],</span><span class="pln">
  exports</span><span class="pun">:</span><span class="pln"> </span><span class="pun">[</span><span class="pln"> </span><span class="typ">AppComponent</span><span class="pln"> </span><span class="pun">],</span><span class="pln">
  entryComponents</span><span class="pun">:</span><span class="pln"> </span><span class="pun">[</span><span class="pln"> </span><span class="typ">AppComponent</span><span class="pln"> </span><span class="pun">]</span><span class="pln">
</span><span class="pun">})</span><span class="pln">
</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">AppModule</span><span class="pln"> </span><span class="pun">{}</span></code>
    </pre></aio-code></code-example><p translation-result="on"><a href="guide/styleguide#toc">回到顶部</a></p><p translation-origin="off"><a href="guide/styleguide#toc">Back to top</a></p><a id="04-09"></a><h3 id="feature-modules" translation-result="on">特性模块<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#feature-modules"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="feature-modules">Feature modules<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#feature-modules"><i class="material-icons">link</i></a></h3><h4 id="style-04-09" translation-result="on">风格 04-09<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-04-09"><i class="material-icons">link</i></a></h4><h4 translation-origin="off" id="style-04-09">Style 04-09<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-04-09"><i class="material-icons">link</i></a></h4><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>为应用中每个明显的特性创建一个 NgModule。</p><p translation-origin="off"><strong>Do</strong> create an NgModule for all distinct features in an application; for example, a <code>Heroes</code> feature.</p></div><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>把特性模块放在与特性区同名的目录中（例如 <code>app/heroes</code>）。</p><p translation-origin="off"><strong>Do</strong> place the feature module in the same named folder as the feature area; for example, in <code>app/heroes</code>.</p></div><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>特性模块的文件名应该能反映出特性区的名字和目录（例如 <code>app/heroes/heroes.module.ts</code>）。</p><p translation-origin="off"><strong>Do</strong> name the feature module file reflecting the name of the feature area and folder; for example, <code>app/heroes/heroes.module.ts</code>.</p></div><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>特性模块的符号名应该能反映出特性区、目录和文件名（例如在 <code>app/heroes/heroes.module.ts</code> 中定义 <code>HeroesModule</code>）。</p><p translation-origin="off"><strong>Do</strong> name the feature module symbol reflecting the name of the feature area, folder, and file; for example, <code>app/heroes/heroes.module.ts</code> defines <code>HeroesModule</code>.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong>特性模块可以对其它模块暴露或隐藏自己的实现。</p><p translation-origin="off"><strong>Why?</strong> A feature module can expose or hide its implementation from other modules.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong>特性模块标记出组成该特性分区的相关组件集合。</p><p translation-origin="off"><strong>Why?</strong> A feature module identifies distinct sets of related components that comprise the feature area.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong>方便路由到特性模块 —— 无论是用主动加载还是惰性加载的方式。</p><p translation-origin="off"><strong>Why?</strong> A feature module can easily be routed to both eagerly and lazily.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong>特性模块在特定的功能和其它应用特性之间定义了清晰的边界。</p><p translation-origin="off"><strong>Why?</strong> A feature module defines clear boundaries between specific functionality and other application features.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong>特性模块帮助澄清开发职责，以便于把这些职责指派给不同的项目组。</p><p translation-origin="off"><strong>Why?</strong> A feature module helps clarify and make it easier to assign development responsibilities to different teams.</p></div><div class="s-why-last"><p translation-result="on"><strong>为何？</strong>特性模块易于隔离，以便测试。</p><p translation-origin="off"><strong>Why?</strong> A feature module can easily be isolated for testing.</p></div><p translation-result="on"><a href="guide/styleguide#toc">回到顶部</a></p><p translation-origin="off"><a href="guide/styleguide#toc">Back to top</a></p><a id="04-10"></a><h3 id="shared-feature-module" translation-result="on">共享特性模块<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#shared-feature-module"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="shared-feature-module">Shared feature module<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#shared-feature-module"><i class="material-icons">link</i></a></h3><h4 id="style-04-10" translation-result="on">风格 04-10<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-04-10"><i class="material-icons">link</i></a></h4><h4 translation-origin="off" id="style-04-10">Style 04-10<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-04-10"><i class="material-icons">link</i></a></h4><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>在 <code>shared</code> 目录中创建名叫 <code>SharedModule</code> 的特性模块（例如在 <code>app/shared/shared.module.ts</code> 中定义 <code>SharedModule</code>）。</p><p translation-origin="off"><strong>Do</strong> create a feature module named <code>SharedModule</code> in a <code>shared</code> folder; for example, <code>app/shared/shared.module.ts</code> defines <code>SharedModule</code>.</p></div><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>在共享模块中声明那些可能被特性模块引用的可复用组件、指令和管道。</p><p translation-origin="off"><strong>Do</strong> declare components, directives, and pipes in a shared module when those items will be re-used and referenced by the components declared in other feature modules.</p></div><div class="s-rule consider"><p translation-result="on"><strong>考虑</strong>把可能在整个应用中到处引用的模块命名为 SharedModule</p><p translation-origin="off"><strong>Consider</strong> using the name SharedModule when the contents of a shared module are referenced across the entire application.</p></div><div class="s-rule avoid"><p translation-result="on"><strong>考虑</strong> <em>不要</em>在共享模块中提供服务。服务通常是单例的，应该在整个应用或一个特定的特性模块中只有一份。 不过也有例外，比如，在下面的范例代码中，注意 <code>SharedModule</code> 提供了 <code>FilterTextService</code>。这里可以这么做，因为该服务是无状态的，也就是说，该服务的消费者不会受到这些新实例的影响。</p><p translation-origin="off"><strong>Consider</strong> <em>not</em> providing services in shared modules. Services are usually singletons that are provided once for the entire application or in a particular feature module. There are exceptions, however. For example, in the sample code that follows, notice that the <code>SharedModule</code> provides <code>FilterTextService</code>. This is acceptable here because the service is stateless;that is, the consumers of the service aren't impacted by new instances.</p></div><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>在 <code>SharedModule</code> 中导入所有模块都需要的资产（例如 <code><a href="api/common/CommonModule" class="code-anchor">CommonModule</a></code> 和 <code><a href="api/forms/FormsModule" class="code-anchor">FormsModule</a></code>）。</p><p translation-origin="off"><strong>Do</strong> import all modules required by the assets in the <code>SharedModule</code>; for example, <code><a href="api/common/CommonModule" class="code-anchor">CommonModule</a></code> and <code><a href="api/forms/FormsModule" class="code-anchor">FormsModule</a></code>.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong> <code>SharedModule</code> 中包含的组件、指令和管道可能需要来自其它公共模块的特性（例如来自 <code><a href="api/common/CommonModule" class="code-anchor">CommonModule</a></code> 中的 <code><a href="api/common/NgForOf" class="code-anchor">ngFor</a></code> 指令）。</p><p translation-origin="off"><strong>Why?</strong> <code>SharedModule</code> will contain components, directives and pipes that may need features from another common module; for example, <code><a href="api/common/NgForOf" class="code-anchor">ngFor</a></code> in <code><a href="api/common/CommonModule" class="code-anchor">CommonModule</a></code>.</p></div><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>在 <code>SharedModule</code> 中声明所有组件、指令和管道。</p><p translation-origin="off"><strong>Do</strong> declare all components, directives, and pipes in the <code>SharedModule</code>.</p></div><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>从 <code>SharedModule</code> 中导出其它特性模块所需的全部符号。</p><p translation-origin="off"><strong>Do</strong> export all symbols from the <code>SharedModule</code> that other feature modules need to use.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong> <code>SharedModule</code> 的存在，能让常用的组件、指令和管道在很多其它模块的组件模板中都自动可用。</p><p translation-origin="off"><strong>Why?</strong> <code>SharedModule</code> exists to make commonly used components, directives and pipes available for use in the templates of components in many other modules.</p></div><div class="s-rule avoid"><p translation-result="on"><strong>避免</strong>在 <code>SharedModule</code> 中指定应用级的单例服务提供商。如果是刻意要得到多个服务单例也行，不过还是要小心。</p><p translation-origin="off"><strong>Avoid</strong> specifying app-wide singleton providers in a <code>SharedModule</code>. Intentional singletons are OK. Take care.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong>惰性加载的特性模块如果导入了这个共享模块，会创建一份自己的服务副本，这可能会导致意料之外的后果。</p><p translation-origin="off"><strong>Why?</strong> A lazy loaded feature module that imports that shared module will make its own copy of the service and likely have undesirable results.</p></div><div class="s-why-last"><p translation-result="on"><strong>为何？</strong>对于单例服务，你不希望每个模块都有自己的实例。 而如果 <code>SharedModule</code> 提供了一个服务，那就有可能发生这种情况。</p><p translation-origin="off"><strong>Why?</strong> You don't want each module to have its own separate instance of singleton services. Yet there is a real danger of that happening if the <code>SharedModule</code> provides a service.</p></div><div class="filetree"><div class="file"><p>src</p></div><div class="children"><div class="file"><p>app</p></div><div class="children"><div class="file"><p>shared</p></div><div class="children"><div class="file"><p>shared.module.ts</p></div><div class="file"><p>init-caps.pipe.ts|spec.ts</p></div><div class="file"><p>filter-text.component.ts|spec.ts</p></div><div class="file"><p>filter-text.service.ts|spec.ts</p></div></div><div class="file"><p>app.component.ts|html|css|spec.ts</p></div><div class="file"><p>app.module.ts</p></div><div class="file"><p>app-routing.module.ts</p></div></div><div class="file"><p>main.ts</p></div><div class="file"><p>index.html</p></div></div><div class="file"><p>...</p></div></div><code-tabs ng-version="9.0.0-rc.11"><div style="display:none"><code-pane header="app/shared/shared.module.ts" path="styleguide/src/04-10/app/shared/shared.module.ts">import { <a href="api/core/NgModule" class="code-anchor">NgModule</a> } from '@angular/core'; import { <a href="api/common/CommonModule" class="code-anchor">CommonModule</a> } from '@angular/common'; import { <a href="api/forms/FormsModule" class="code-anchor">FormsModule</a> } from '@angular/forms'; import { FilterTextComponent } from './filter-text/filter-text.component'; import { FilterTextService } from './filter-text/filter-text.service'; import { InitCapsPipe } from './init-caps.pipe'; @<a href="api/core/NgModule" class="code-anchor">NgModule</a>({ imports: [<a href="api/common/CommonModule" class="code-anchor">CommonModule</a>, <a href="api/forms/FormsModule" class="code-anchor">FormsModule</a>], declarations: [ FilterTextComponent, InitCapsPipe ], providers: [FilterTextService], exports: [ <a href="api/common/CommonModule" class="code-anchor">CommonModule</a>, <a href="api/forms/FormsModule" class="code-anchor">FormsModule</a>, FilterTextComponent, InitCapsPipe ] }) export class SharedModule { }</code-pane><code-pane header="app/shared/init-caps.pipe.ts" path="styleguide/src/04-10/app/shared/init-caps.pipe.ts">import { <a href="api/core/Pipe" class="code-anchor">Pipe</a>, <a href="api/core/PipeTransform" class="code-anchor">PipeTransform</a> } from '@angular/core'; @<a href="api/core/Pipe" class="code-anchor">Pipe</a>({ name: 'initCaps' }) export class InitCapsPipe implements <a href="api/core/PipeTransform" class="code-anchor">PipeTransform</a> { transform = (value: string) =&gt; value; }</code-pane><code-pane header="app/shared/filter-text/filter-text.component.ts" path="styleguide/src/04-10/app/shared/filter-text/filter-text.component.ts">import { <a href="api/core/Component" class="code-anchor">Component</a>, <a href="api/core/EventEmitter" class="code-anchor">EventEmitter</a>, <a href="api/core/Output" class="code-anchor">Output</a> } from '@angular/core'; @<a href="api/core/Component" class="code-anchor">Component</a>({ selector: 'toh-filter-text', template: '&lt;input type="text" id="filterText" [(<a href="api/forms/NgModel" class="code-anchor">ngModel</a>)]="filter" (keyup)="filterChanged($event)" /&gt;' }) export class FilterTextComponent { @<a href="api/core/Output" class="code-anchor">Output</a>() changed: <a href="api/core/EventEmitter" class="code-anchor">EventEmitter</a>&lt;string&gt;; filter: string; constructor() { this.changed = new <a href="api/core/EventEmitter" class="code-anchor">EventEmitter</a>&lt;string&gt;(); } clear() { this.filter = ''; } filterChanged(event: any) { event.preventDefault(); console.log(`Filter Changed: ${this.filter}`); this.changed.emit(this.filter); } }</code-pane><code-pane header="app/shared/filter-text/filter-text.service.ts" path="styleguide/src/04-10/app/shared/filter-text/filter-text.service.ts">import { <a href="api/core/Injectable" class="code-anchor">Injectable</a> } from '@angular/core'; @<a href="api/core/Injectable" class="code-anchor">Injectable</a>() export class FilterTextService { constructor() { console.log('Created an instance of FilterTextService'); } filter(data: string, props: Array&lt;string&gt;, originalList: Array&lt;any&gt;) { let filteredList: any[]; if (data &amp;&amp; props &amp;&amp; originalList) { data = data.toLowerCase(); let filtered = originalList.filter(<a href="api/core/IterableChangeRecord#item" class="code-anchor">item</a> =&gt; { let match = false; for (let prop of props) { if (<a href="api/core/IterableChangeRecord#item" class="code-anchor">item</a>[prop].toString().toLowerCase().indexOf(data) &gt; -1) { match = true; break; } }; return match; }); filteredList = filtered; } else { filteredList = originalList; } return filteredList; } }</code-pane><code-pane header="app/heroes/heroes.component.ts" path="styleguide/src/04-10/app/heroes/heroes.component.ts">import { <a href="api/core/Component" class="code-anchor">Component</a> } from '@angular/core'; import { FilterTextService } from '../shared/filter-text/filter-text.service'; @<a href="api/core/Component" class="code-anchor">Component</a>({ selector: 'toh-heroes', <a href="api/core/Component#templateUrl" class="code-anchor">templateUrl</a>: './heroes.component.html' }) export class HeroesComponent { heroes = [ { id: 1, name: 'Windstorm' }, { id: 2, name: 'Bombasto' }, { id: 3, name: 'Magneta' }, { id: 4, name: 'Tornado' } ]; filteredHeroes = this.heroes; constructor(private filterService: FilterTextService) { } filterChanged(searchText: string) { this.filteredHeroes = this.filterService.filter(searchText, ['id', 'name'], this.heroes); } }</code-pane><code-pane header="app/heroes/heroes.component.html" path="styleguide/src/04-10/app/heroes/heroes.component.html">&lt;div&gt;This is heroes component&lt;/div&gt; &lt;ul&gt; &lt;li *<a href="api/common/NgForOf" class="code-anchor">ngFor</a>="let hero of filteredHeroes"&gt; {{hero.name}} &lt;/li&gt; &lt;/ul&gt; &lt;toh-filter-text (changed)="filterChanged($event)"&gt;&lt;/toh-filter-text&gt;</code-pane></div><mat-card class="mat-card"><mat-tab-group class="mat-tab-group code-tab-group mat-primary"><mat-tab-header class="mat-tab-header mat-tab-header-pagination-controls-enabled"><div aria-hidden="true" mat-ripple="" class="mat-ripple mat-tab-header-pagination mat-tab-header-pagination-before mat-elevation-z4 mat-tab-header-pagination-disabled"><div class="mat-tab-header-pagination-chevron"></div></div><div class="mat-tab-label-container"><div role="tablist" class="mat-tab-list" style="transform:translateX(0)"><div class="mat-tab-labels"><div role="tab" mattablabelwrapper="" mat-ripple="" cdkmonitorelementfocus="" class="mat-ripple mat-tab-label mat-tab-label-active ng-star-inserted" id="mat-tab-label-2-0" tabindex="0" aria-posinset="1" aria-setsize="6" aria-controls="mat-tab-content-2-0" aria-selected="true" aria-disabled="false"><div class="mat-tab-label-content"><span class="ng-star-inserted">app/shared/shared.module.ts</span></div></div><div role="tab" mattablabelwrapper="" mat-ripple="" cdkmonitorelementfocus="" class="mat-ripple mat-tab-label ng-star-inserted" id="mat-tab-label-2-1" tabindex="-1" aria-posinset="2" aria-setsize="6" aria-controls="mat-tab-content-2-1" aria-selected="false" aria-disabled="false"><div class="mat-tab-label-content"><span class="ng-star-inserted">app/shared/init-caps.pipe.ts</span></div></div><div role="tab" mattablabelwrapper="" mat-ripple="" cdkmonitorelementfocus="" class="mat-ripple mat-tab-label ng-star-inserted" id="mat-tab-label-2-2" tabindex="-1" aria-posinset="3" aria-setsize="6" aria-controls="mat-tab-content-2-2" aria-selected="false" aria-disabled="false"><div class="mat-tab-label-content"><span class="ng-star-inserted">app/shared/filter-text/filter-text.component.ts</span></div></div><div role="tab" mattablabelwrapper="" mat-ripple="" cdkmonitorelementfocus="" class="mat-ripple mat-tab-label ng-star-inserted" id="mat-tab-label-2-3" tabindex="-1" aria-posinset="4" aria-setsize="6" aria-controls="mat-tab-content-2-3" aria-selected="false" aria-disabled="false"><div class="mat-tab-label-content"><span class="ng-star-inserted">app/shared/filter-text/filter-text.service.ts</span></div></div><div role="tab" mattablabelwrapper="" mat-ripple="" cdkmonitorelementfocus="" class="mat-ripple mat-tab-label ng-star-inserted" id="mat-tab-label-2-4" tabindex="-1" aria-posinset="5" aria-setsize="6" aria-controls="mat-tab-content-2-4" aria-selected="false" aria-disabled="false"><div class="mat-tab-label-content"><span class="ng-star-inserted">app/heroes/heroes.component.ts</span></div></div><div role="tab" mattablabelwrapper="" mat-ripple="" cdkmonitorelementfocus="" class="mat-ripple mat-tab-label ng-star-inserted" id="mat-tab-label-2-5" tabindex="-1" aria-posinset="6" aria-setsize="6" aria-controls="mat-tab-content-2-5" aria-selected="false" aria-disabled="false"><div class="mat-tab-label-content"><span class="ng-star-inserted">app/heroes/heroes.component.html</span></div></div></div><mat-ink-bar class="mat-ink-bar" style="visibility:visible;left:0;width:229px"></mat-ink-bar></div></div><div aria-hidden="true" mat-ripple="" class="mat-ripple mat-tab-header-pagination mat-tab-header-pagination-after mat-elevation-z4"><div class="mat-tab-header-pagination-chevron"></div></div></mat-tab-header><div class="mat-tab-body-wrapper"><mat-tab-body role="tabpanel" class="mat-tab-body ng-tns-c50-11 mat-tab-body-active ng-star-inserted" id="mat-tab-content-2-0" aria-labelledby="mat-tab-label-2-0"><div class="mat-tab-body-content ng-tns-c50-11 ng-trigger ng-trigger-translateTab" style="transform:none"><aio-code class="ng-star-inserted" style=""><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from app/shared/shared.module.ts">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="kwd">import</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><a href="api/core/NgModule" class="code-anchor"><span class="typ">NgModule</span></a><span class="pln"> </span><span class="pun">}</span><span class="pln">      </span><span class="kwd">from</span><span class="pln"> </span><span class="str">'@angular/core'</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">import</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><a href="api/common/CommonModule" class="code-anchor"><span class="typ">CommonModule</span></a><span class="pln"> </span><span class="pun">}</span><span class="pln">  </span><span class="kwd">from</span><span class="pln"> </span><span class="str">'@angular/common'</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">import</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><a href="api/forms/FormsModule" class="code-anchor"><span class="typ">FormsModule</span></a><span class="pln"> </span><span class="pun">}</span><span class="pln">   </span><span class="kwd">from</span><span class="pln"> </span><span class="str">'@angular/forms'</span><span class="pun">;</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="typ">FilterTextComponent</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">from</span><span class="pln"> </span><span class="str">'./filter-text/filter-text.component'</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">import</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="typ">FilterTextService</span><span class="pln"> </span><span class="pun">}</span><span class="pln">   </span><span class="kwd">from</span><span class="pln"> </span><span class="str">'./filter-text/filter-text.service'</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">import</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="typ">InitCapsPipe</span><span class="pln"> </span><span class="pun">}</span><span class="pln">        </span><span class="kwd">from</span><span class="pln"> </span><span class="str">'./init-caps.pipe'</span><span class="pun">;</span><span class="pln">

</span><span class="lit">@</span><a href="api/core/NgModule" class="code-anchor"><span class="lit">NgModule</span></a><span class="pun">({</span><span class="pln">
  imports</span><span class="pun">:</span><span class="pln"> </span><span class="pun">[</span><a href="api/common/CommonModule" class="code-anchor"><span class="typ">CommonModule</span></a><span class="pun">,</span><span class="pln"> </span><a href="api/forms/FormsModule" class="code-anchor"><span class="typ">FormsModule</span></a><span class="pun">],</span><span class="pln">
  declarations</span><span class="pun">:</span><span class="pln"> </span><span class="pun">[</span><span class="pln">
    </span><span class="typ">FilterTextComponent</span><span class="pun">,</span><span class="pln">
    </span><span class="typ">InitCapsPipe</span><span class="pln">
  </span><span class="pun">],</span><span class="pln">
  providers</span><span class="pun">:</span><span class="pln"> </span><span class="pun">[</span><span class="typ">FilterTextService</span><span class="pun">],</span><span class="pln">
  exports</span><span class="pun">:</span><span class="pln"> </span><span class="pun">[</span><span class="pln">
    </span><a href="api/common/CommonModule" class="code-anchor"><span class="typ">CommonModule</span></a><span class="pun">,</span><span class="pln">
    </span><a href="api/forms/FormsModule" class="code-anchor"><span class="typ">FormsModule</span></a><span class="pun">,</span><span class="pln">
    </span><span class="typ">FilterTextComponent</span><span class="pun">,</span><span class="pln">
    </span><span class="typ">InitCapsPipe</span><span class="pln">
  </span><span class="pun">]</span><span class="pln">
</span><span class="pun">})</span><span class="pln">
</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">SharedModule</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="pun">}</span></code>
    </pre></aio-code></div></mat-tab-body><mat-tab-body role="tabpanel" class="mat-tab-body ng-tns-c50-12 ng-star-inserted" id="mat-tab-content-2-1" aria-labelledby="mat-tab-label-2-1"><div class="mat-tab-body-content ng-tns-c50-12 ng-trigger ng-trigger-translateTab" style="transform:translate3d(100%,0,0);min-height:1px"></div></mat-tab-body><mat-tab-body role="tabpanel" class="mat-tab-body ng-tns-c50-13 ng-star-inserted" id="mat-tab-content-2-2" aria-labelledby="mat-tab-label-2-2"><div class="mat-tab-body-content ng-tns-c50-13 ng-trigger ng-trigger-translateTab" style="transform:translate3d(100%,0,0);min-height:1px"></div></mat-tab-body><mat-tab-body role="tabpanel" class="mat-tab-body ng-tns-c50-14 ng-star-inserted" id="mat-tab-content-2-3" aria-labelledby="mat-tab-label-2-3"><div class="mat-tab-body-content ng-tns-c50-14 ng-trigger ng-trigger-translateTab" style="transform:translate3d(100%,0,0);min-height:1px"></div></mat-tab-body><mat-tab-body role="tabpanel" class="mat-tab-body ng-tns-c50-15 ng-star-inserted" id="mat-tab-content-2-4" aria-labelledby="mat-tab-label-2-4"><div class="mat-tab-body-content ng-tns-c50-15 ng-trigger ng-trigger-translateTab" style="transform:translate3d(100%,0,0);min-height:1px"></div></mat-tab-body><mat-tab-body role="tabpanel" class="mat-tab-body ng-tns-c50-16 ng-star-inserted" id="mat-tab-content-2-5" aria-labelledby="mat-tab-label-2-5"><div class="mat-tab-body-content ng-tns-c50-16 ng-trigger ng-trigger-translateTab" style="transform:translate3d(100%,0,0);min-height:1px"></div></mat-tab-body></div></mat-tab-group></mat-card></code-tabs><p translation-result="on"><a href="guide/styleguide#toc">回到顶部</a></p><p translation-origin="off"><a href="guide/styleguide#toc">Back to top</a></p><a id="04-11"></a><h3 id="lazy-loaded-folders" translation-result="on">惰性加载文件夹<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#lazy-loaded-folders"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="lazy-loaded-folders">Lazy Loaded folders<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#lazy-loaded-folders"><i class="material-icons">link</i></a></h3><h4 id="style-04-11" translation-result="on">风格 04-11<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-04-11"><i class="material-icons">link</i></a></h4><h4 translation-origin="off" id="style-04-11">Style 04-11<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-04-11"><i class="material-icons">link</i></a></h4><p translation-result="on">某些边界清晰的应用特性或工作流可以做成<em>惰性加载</em>或<em>按需加载</em>的，而不用总是随着应用启动。</p><p translation-origin="off">A distinct application feature or workflow may be <em>lazy loaded</em> or <em>loaded on demand</em> rather than when the application starts.</p><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>把惰性加载特性下的内容放进<em>惰性加载目录</em>中。 典型的<em>惰性加载目录</em>包含<em>路由组件</em>及其子组件以及与它们有关的那些资产和模块。</p><p translation-origin="off"><strong>Do</strong> put the contents of lazy loaded features in a <em>lazy loaded folder</em>. A typical <em>lazy loaded folder</em> contains a <em>routing component</em>, its child components, and their related assets and modules.</p></div><div class="s-why-last"><p translation-result="on"><strong>为何？</strong>这种目录让标识和隔离这些特性内容变得更轻松。</p><p translation-origin="off"><strong>Why?</strong> The folder makes it easy to identify and isolate the feature content.</p></div><p translation-result="on"><a href="guide/styleguide#toc">回到顶部</a></p><p translation-origin="off"><a href="guide/styleguide#toc">Back to top</a></p><a id="04-12"></a><h3 id="never-directly-import-lazy-loaded-folders" translation-result="on">永远不要直接导入惰性加载的目录<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#never-directly-import-lazy-loaded-folders"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="never-directly-import-lazy-loaded-folders">Never directly import lazy loaded folders<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#never-directly-import-lazy-loaded-folders"><i class="material-icons">link</i></a></h3><h4 id="style-04-12" translation-result="on">样式 04-14<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-04-12"><i class="material-icons">link</i></a></h4><h4 translation-origin="off" id="style-04-12">Style 04-12<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-04-12"><i class="material-icons">link</i></a></h4><div class="s-rule avoid"><p translation-result="on"><strong>避免</strong>让兄弟模块和父模块直接导入<em>惰性加载特性</em>中的模块。</p><p translation-origin="off"><strong>Avoid</strong> allowing modules in sibling and parent folders to directly import a module in a <em>lazy loaded feature</em>.</p></div><div class="s-why-last"><p translation-result="on"><strong>为何？</strong>直接导入并使用此模块会立即加载它，而原本的设计意图是按需加载它。</p><p translation-origin="off"><strong>Why?</strong> Directly importing and using a module will load it immediately when the intention is to load it on demand.</p></div><p translation-result="on"><a href="guide/styleguide#toc">回到顶部</a></p><p translation-origin="off"><a href="guide/styleguide#toc">Back to top</a></p><h2 id="components" translation-result="on">组件<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#components"><i class="material-icons">link</i></a></h2><h2 translation-origin="off" id="components">Components<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#components"><i class="material-icons">link</i></a></h2><a id="05-03"></a><h3 id="components-as-elements" translation-result="on">把组件当做元素<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#components-as-elements"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="components-as-elements">Components as elements<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#components-as-elements"><i class="material-icons">link</i></a></h3><h4 id="style-05-03" translation-result="on">风格 05-03<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-05-03"><i class="material-icons">link</i></a></h4><h4 translation-origin="off" id="style-05-03">Style 05-03<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-05-03"><i class="material-icons">link</i></a></h4><div class="s-rule do"><p translation-result="on"><strong>考虑</strong>给组件一个<em>元素</em>选择器，而不是<em>属性</em>或<em>类</em>选择器。</p><p translation-origin="off"><strong>Consider</strong> giving components an <em>element</em> selector, as opposed to <em>attribute</em> or <em>class</em> selectors.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong>组件有很多包含 HTML 以及可选 Angular 模板语法的模板。 它们显示内容。开发人员会把组件像原生 HTML 元素和 WebComponents 一样放进页面中。</p><p translation-origin="off"><strong>Why?</strong> Components have templates containing HTML and optional Angular template syntax. They display content. Developers place components on the page as they would native HTML elements and web components.</p></div><div class="s-why-last"><p translation-result="on"><strong>为何？</strong>查看组件模板的 HTML 时，更容易识别一个符号是组件还是指令。</p><p translation-origin="off"><strong>Why?</strong> It is easier to recognize that a symbol is a component by looking at the template's html.</p></div><div class="alert is-helpful"><p translation-result="on">少数情况下，你要为组件使用属性选择器，比如你要加强某个内置元素时。 比如，<a href="https://material.angular.cn/components/button/overview">Material Design 组件库</a>就会对 <code>&lt;button mat-button&gt;</code> 使用这项技术。不过，你不应该在自定义组件上使用这项技术。</p><p translation-origin="off">There are a few cases where you give a component an attribute, such as when you want to augment a built-in element. For example, <a href="https://material.angular.io/components/button/overview">Material Design</a> uses this technique with <code>&lt;button mat-button&gt;</code>. However, you wouldn't use this technique on a custom element.</p></div><code-example path="styleguide/src/05-03/app/heroes/shared/hero-button/hero-button.component.avoid.ts" region="example" header="app/heroes/hero-button/hero-button.component.ts" ng-version="9.0.0-rc.11" class="avoidFile"><div style="display:none">/* avoid */ @<a href="api/core/Component" class="code-anchor">Component</a>({ selector: '[tohHeroButton]', <a href="api/core/Component#templateUrl" class="code-anchor">templateUrl</a>: './hero-button.component.html' }) export class HeroButtonComponent {}</div><header class="ng-star-inserted">app/heroes/hero-button/hero-button.component.ts</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from app/heroes/hero-button/hero-button.component.ts">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="com">/* avoid */</span><span class="pln">

</span><span class="lit">@</span><a href="api/core/Component" class="code-anchor"><span class="lit">Component</span></a><span class="pun">({</span><span class="pln">
  selector</span><span class="pun">:</span><span class="pln"> </span><span class="str">'[tohHeroButton]'</span><span class="pun">,</span><span class="pln">
  </span><a href="api/core/Component#templateUrl" class="code-anchor"><span class="pln">templateUrl</span></a><span class="pun">:</span><span class="pln"> </span><span class="str">'./hero-button.component.html'</span><span class="pln">
</span><span class="pun">})</span><span class="pln">
</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">HeroButtonComponent</span><span class="pln"> </span><span class="pun">{}</span></code>
    </pre></aio-code></code-example><code-example path="styleguide/src/05-03/app/app.component.avoid.html" header="app/app.component.html" ng-version="9.0.0-rc.11" class="avoidFile"><div style="display:none">&lt;!-- avoid --&gt; &lt;div tohHeroButton&gt;&lt;/div&gt;</div><header class="ng-star-inserted">app/app.component.html</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from app/app.component.html">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="com">&lt;!-- avoid --&gt;</span><span class="pln">

</span><span class="tag">&lt;div</span><span class="pln"> </span><span class="atn">tohHeroButton</span><span class="tag">&gt;&lt;/div&gt;</span></code>
    </pre></aio-code></code-example><code-tabs ng-version="9.0.0-rc.11"><div style="display:none"><code-pane header="app/heroes/shared/hero-button/hero-button.component.ts" path="styleguide/src/05-03/app/heroes/shared/hero-button/hero-button.component.ts" region="example">@<a href="api/core/Component" class="code-anchor">Component</a>({ selector: 'toh-hero-button', <a href="api/core/Component#templateUrl" class="code-anchor">templateUrl</a>: './hero-button.component.html' }) export class HeroButtonComponent {}</code-pane><code-pane header="app/app.component.html" path="styleguide/src/05-03/app/app.component.html">&lt;toh-hero-button&gt;&lt;/toh-hero-button&gt;</code-pane></div><mat-card class="mat-card"><mat-tab-group class="mat-tab-group code-tab-group mat-primary"><mat-tab-header class="mat-tab-header"><div aria-hidden="true" mat-ripple="" class="mat-ripple mat-tab-header-pagination mat-tab-header-pagination-before mat-elevation-z4 mat-tab-header-pagination-disabled"><div class="mat-tab-header-pagination-chevron"></div></div><div class="mat-tab-label-container"><div role="tablist" class="mat-tab-list" style="transform:translateX(0)"><div class="mat-tab-labels"><div role="tab" mattablabelwrapper="" mat-ripple="" cdkmonitorelementfocus="" class="mat-ripple mat-tab-label mat-tab-label-active ng-star-inserted" id="mat-tab-label-3-0" tabindex="0" aria-posinset="1" aria-setsize="2" aria-controls="mat-tab-content-3-0" aria-selected="true" aria-disabled="false"><div class="mat-tab-label-content"><span class="ng-star-inserted">app/heroes/shared/hero-button/hero-button.component.ts</span></div></div><div role="tab" mattablabelwrapper="" mat-ripple="" cdkmonitorelementfocus="" class="mat-ripple mat-tab-label ng-star-inserted" id="mat-tab-label-3-1" tabindex="-1" aria-posinset="2" aria-setsize="2" aria-controls="mat-tab-content-3-1" aria-selected="false" aria-disabled="false"><div class="mat-tab-label-content"><span class="ng-star-inserted">app/app.component.html</span></div></div></div><mat-ink-bar class="mat-ink-bar" style="visibility:visible;left:0;width:404px"></mat-ink-bar></div></div><div aria-hidden="true" mat-ripple="" class="mat-ripple mat-tab-header-pagination mat-tab-header-pagination-after mat-elevation-z4 mat-tab-header-pagination-disabled"><div class="mat-tab-header-pagination-chevron"></div></div></mat-tab-header><div class="mat-tab-body-wrapper"><mat-tab-body role="tabpanel" class="mat-tab-body ng-tns-c50-17 mat-tab-body-active ng-star-inserted" id="mat-tab-content-3-0" aria-labelledby="mat-tab-label-3-0"><div class="mat-tab-body-content ng-tns-c50-17 ng-trigger ng-trigger-translateTab" style="transform:none"><aio-code class="ng-star-inserted" style=""><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from app/heroes/shared/hero-button/hero-button.component.ts">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="lit">@</span><a href="api/core/Component" class="code-anchor"><span class="lit">Component</span></a><span class="pun">({</span><span class="pln">
  selector</span><span class="pun">:</span><span class="pln"> </span><span class="str">'toh-hero-button'</span><span class="pun">,</span><span class="pln">
  </span><a href="api/core/Component#templateUrl" class="code-anchor"><span class="pln">templateUrl</span></a><span class="pun">:</span><span class="pln"> </span><span class="str">'./hero-button.component.html'</span><span class="pln">
</span><span class="pun">})</span><span class="pln">
</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">HeroButtonComponent</span><span class="pln"> </span><span class="pun">{}</span></code>
    </pre></aio-code></div></mat-tab-body><mat-tab-body role="tabpanel" class="mat-tab-body ng-tns-c50-18 ng-star-inserted" id="mat-tab-content-3-1" aria-labelledby="mat-tab-label-3-1"><div class="mat-tab-body-content ng-tns-c50-18 ng-trigger ng-trigger-translateTab" style="transform:translate3d(100%,0,0);min-height:1px"></div></mat-tab-body></div></mat-tab-group></mat-card></code-tabs><p translation-result="on"><a href="guide/styleguide#toc">回到顶部</a></p><p translation-origin="off"><a href="guide/styleguide#toc">Back to top</a></p><a id="05-04"></a><h3 id="extract-templates-and-styles-to-their-own-files" translation-result="on">把模板和样式提取到它们自己的文件<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#extract-templates-and-styles-to-their-own-files"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="extract-templates-and-styles-to-their-own-files">Extract templates and styles to their own files<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#extract-templates-and-styles-to-their-own-files"><i class="material-icons">link</i></a></h3><h4 id="style-05-04" translation-result="on">风格 05-04<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-05-04"><i class="material-icons">link</i></a></h4><h4 translation-origin="off" id="style-05-04">Style 05-04<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-05-04"><i class="material-icons">link</i></a></h4><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>当超过 3 行时，把模板和样式提取到一个单独的文件。</p><p translation-origin="off"><strong>Do</strong> extract templates and styles into a separate file, when more than 3 lines.</p></div><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>把模板文件命名为 <code>[component-name].component.html</code>，其中，[component-name] 是组件名。</p><p translation-origin="off"><strong>Do</strong> name the template file <code>[component-name].component.html</code>, where [component-name] is the component name.</p></div><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>把样式文件命名为 <code>[component-name].component.css</code>，其中，[component-name] 是组件名。</p><p translation-origin="off"><strong>Do</strong> name the style file <code>[component-name].component.css</code>, where [component-name] is the component name.</p></div><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>指定<em>相对于模块的</em> URL ，给它加上 <code>./</code> 前缀。</p><p translation-origin="off"><strong>Do</strong> specify <em>component-relative</em> URLs, prefixed with <code>./</code>.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong>巨大的、内联的模板和样式表会遮盖组件的意图和实现方式，削弱可读性和可维护性。</p><p translation-origin="off"><strong>Why?</strong> Large, inline templates and styles obscure the component's purpose and implementation, reducing readability and maintainability.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong>在多数编辑器中，编写内联的模板和样式表时都无法使用语法提示和代码片段功能。 Angular 的 TypeScript 语言服务（即将到来）可以帮助那些编辑器在编写 HTML 模板时克服这一缺陷，但对 CSS 样式没有帮助。</p><p translation-origin="off"><strong>Why?</strong> In most editors, syntax hints and code snippets aren't available when developing inline templates and styles. The Angular TypeScript Language Service (forthcoming) promises to overcome this deficiency for HTML templates in those editors that support it; it won't help with CSS styles.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong>当你移动组件文件时，相对于组件的 URL 不需要修改，因为这些文件始终会在一起。</p><p translation-origin="off"><strong>Why?</strong> A <em>component relative</em> URL requires no change when you move the component files, as long as the files stay together.</p></div><div class="s-why-last"><p translation-result="on"><strong>为何？</strong><code>./</code> 前缀是相对 URL 的标准语法，不必依赖 Angular 的特殊处理，如果没有前缀则不行。</p><p translation-origin="off"><strong>Why?</strong> The <code>./</code> prefix is standard syntax for relative URLs; don't depend on Angular's current ability to do without that prefix.</p></div><code-example path="styleguide/src/05-04/app/heroes/heroes.component.avoid.ts" region="example" header="app/heroes/heroes.component.ts" ng-version="9.0.0-rc.11" class="avoidFile"><div style="display:none">/* avoid */ @<a href="api/core/Component" class="code-anchor">Component</a>({ selector: 'toh-heroes', template: ` &lt;div&gt; &lt;h2&gt;My Heroes&lt;/h2&gt; &lt;ul class="heroes"&gt; &lt;li *<a href="api/common/NgForOf" class="code-anchor">ngFor</a>="let hero of heroes | async" (click)="selectedHero=hero"&gt; &lt;span class="badge"&gt;{{hero.id}}&lt;/span&gt; {{hero.name}} &lt;/li&gt; &lt;/ul&gt; &lt;div *<a href="api/common/NgIf" class="code-anchor">ngIf</a>="selectedHero"&gt; &lt;h2&gt;{{selectedHero.name | <a href="api/common/UpperCasePipe" class="code-anchor">uppercase</a>}} is my hero&lt;/h2&gt; &lt;/div&gt; &lt;/div&gt; `, styles: [` .heroes { margin: 0 0 2em 0; list-style-type: none; padding: 0; width: 15em; } .heroes li { cursor: pointer; <a href="api/router/Scroll#position" class="code-anchor">position</a>: relative; left: 0; background-color: #EEE; margin: .5em; padding: .3em 0; height: 1.6em; border-radius: 4px; } .heroes .badge { display: inline-block; font-size: small; color: white; padding: 0.8em 0.7em 0 0.7em; background-color: #607D8B; line-height: 1em; <a href="api/router/Scroll#position" class="code-anchor">position</a>: relative; left: -1px; top: -4px; height: 1.8em; margin-right: .8em; border-radius: 4px 0 0 4px; } `] }) export class HeroesComponent implements <a href="api/core/OnInit" class="code-anchor">OnInit</a> { heroes: Observable&lt;Hero[]&gt;; selectedHero: Hero; constructor(private heroService: HeroService) { } ngOnInit() { this.heroes = this.heroService.getHeroes(); } }</div><header class="ng-star-inserted">app/heroes/heroes.component.ts</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from app/heroes/heroes.component.ts">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="com">/* avoid */</span><span class="pln">

</span><span class="lit">@</span><a href="api/core/Component" class="code-anchor"><span class="lit">Component</span></a><span class="pun">({</span><span class="pln">
  selector</span><span class="pun">:</span><span class="pln"> </span><span class="str">'toh-heroes'</span><span class="pun">,</span><span class="pln">
  </span><span class="kwd">template</span><span class="pun">:</span><span class="pln"> </span><span class="str">`
    &lt;div&gt;
      &lt;h2&gt;My Heroes&lt;/h2&gt;
      &lt;ul class="heroes"&gt;
        &lt;li *</span><a href="api/common/NgForOf" class="code-anchor"><span class="str">ngFor</span></a><span class="str">="let hero of heroes | async" (click)="selectedHero=hero"&gt;
          &lt;span class="badge"&gt;{{hero.id}}&lt;/span&gt; {{hero.name}}
        &lt;/li&gt;
      &lt;/ul&gt;
      &lt;div *</span><a href="api/common/NgIf" class="code-anchor"><span class="str">ngIf</span></a><span class="str">="selectedHero"&gt;
        &lt;h2&gt;{{selectedHero.name | </span><a href="api/common/UpperCasePipe" class="code-anchor"><span class="str">uppercase</span></a><span class="str">}} is my hero&lt;/h2&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  `</span><span class="pun">,</span><span class="pln">
  styles</span><span class="pun">:</span><span class="pln"> </span><span class="pun">[</span><span class="str">`
    .heroes {
      margin: 0 0 2em 0;
      list-style-type: none;
      padding: 0;
      width: 15em;
    }
    .heroes li {
      cursor: pointer;
      </span><a href="api/router/Scroll#position" class="code-anchor"><span class="str">position</span></a><span class="str">: relative;
      left: 0;
      background-color: #EEE;
      margin: .5em;
      padding: .3em 0;
      height: 1.6em;
      border-radius: 4px;
    }
    .heroes .badge {
      display: inline-block;
      font-size: small;
      color: white;
      padding: 0.8em 0.7em 0 0.7em;
      background-color: #607D8B;
      line-height: 1em;
      </span><a href="api/router/Scroll#position" class="code-anchor"><span class="str">position</span></a><span class="str">: relative;
      left: -1px;
      top: -4px;
      height: 1.8em;
      margin-right: .8em;
      border-radius: 4px 0 0 4px;
    }
  `</span><span class="pun">]</span><span class="pln">
</span><span class="pun">})</span><span class="pln">
</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">HeroesComponent</span><span class="pln"> </span><span class="kwd">implements</span><span class="pln"> </span><a href="api/core/OnInit" class="code-anchor"><span class="typ">OnInit</span></a><span class="pln"> </span><span class="pun">{</span><span class="pln">
  heroes</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Observable</span><span class="pun">&lt;</span><span class="typ">Hero</span><span class="pun">[]&gt;;</span><span class="pln">
  selectedHero</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Hero</span><span class="pun">;</span><span class="pln">

 </span><span class="kwd">constructor</span><span class="pun">(</span><span class="kwd">private</span><span class="pln"> heroService</span><span class="pun">:</span><span class="pln"> </span><span class="typ">HeroService</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="pun">}</span><span class="pln">

  ngOnInit</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">heroes </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">heroService</span><span class="pun">.</span><span class="pln">getHeroes</span><span class="pun">();</span><span class="pln">
  </span><span class="pun">}</span><span class="pln">
</span><span class="pun">}</span></code>
    </pre></aio-code></code-example><code-tabs ng-version="9.0.0-rc.11"><div style="display:none"><code-pane header="app/heroes/heroes.component.ts" path="styleguide/src/05-04/app/heroes/heroes.component.ts" region="example">@<a href="api/core/Component" class="code-anchor">Component</a>({ selector: 'toh-heroes', <a href="api/core/Component#templateUrl" class="code-anchor">templateUrl</a>: './heroes.component.html', <a href="api/core/Component#styleUrls" class="code-anchor">styleUrls</a>: ['./heroes.component.css'] }) export class HeroesComponent implements <a href="api/core/OnInit" class="code-anchor">OnInit</a> { heroes: Observable&lt;Hero[]&gt;; selectedHero: Hero; constructor(private heroService: HeroService) { } ngOnInit() { this.heroes = this.heroService.getHeroes(); } }</code-pane><code-pane header="app/heroes/heroes.component.html" path="styleguide/src/05-04/app/heroes/heroes.component.html">&lt;div&gt; &lt;h2&gt;My Heroes&lt;/h2&gt; &lt;ul class="heroes"&gt; &lt;li *<a href="api/common/NgForOf" class="code-anchor">ngFor</a>="let hero of heroes | async" (click)="selectedHero=hero"&gt; &lt;span class="badge"&gt;{{hero.id}}&lt;/span&gt; {{hero.name}} &lt;/li&gt; &lt;/ul&gt; &lt;div *<a href="api/common/NgIf" class="code-anchor">ngIf</a>="selectedHero"&gt; &lt;h2&gt;{{selectedHero.name | <a href="api/common/UpperCasePipe" class="code-anchor">uppercase</a>}} is my hero&lt;/h2&gt; &lt;/div&gt; &lt;/div&gt;</code-pane><code-pane header="app/heroes/heroes.component.css" path="styleguide/src/05-04/app/heroes/heroes.component.css">.heroes { margin: 0 0 2em 0; list-style-type: none; padding: 0; width: 15em; } .heroes li { cursor: pointer; <a href="api/router/Scroll#position" class="code-anchor">position</a>: relative; left: 0; background-color: #EEE; margin: .5em; padding: .3em 0; height: 1.6em; border-radius: 4px; } .heroes .badge { display: inline-block; font-size: small; color: white; padding: 0.8em 0.7em 0 0.7em; background-color: #607D8B; line-height: 1em; <a href="api/router/Scroll#position" class="code-anchor">position</a>: relative; left: -1px; top: -4px; height: 1.8em; margin-right: .8em; border-radius: 4px 0 0 4px; }</code-pane></div><mat-card class="mat-card"><mat-tab-group class="mat-tab-group code-tab-group mat-primary"><mat-tab-header class="mat-tab-header mat-tab-header-pagination-controls-enabled"><div aria-hidden="true" mat-ripple="" class="mat-ripple mat-tab-header-pagination mat-tab-header-pagination-before mat-elevation-z4 mat-tab-header-pagination-disabled"><div class="mat-tab-header-pagination-chevron"></div></div><div class="mat-tab-label-container"><div role="tablist" class="mat-tab-list" style="transform:translateX(0)"><div class="mat-tab-labels"><div role="tab" mattablabelwrapper="" mat-ripple="" cdkmonitorelementfocus="" class="mat-ripple mat-tab-label mat-tab-label-active ng-star-inserted" id="mat-tab-label-4-0" tabindex="0" aria-posinset="1" aria-setsize="3" aria-controls="mat-tab-content-4-0" aria-selected="true" aria-disabled="false"><div class="mat-tab-label-content"><span class="ng-star-inserted">app/heroes/heroes.component.ts</span></div></div><div role="tab" mattablabelwrapper="" mat-ripple="" cdkmonitorelementfocus="" class="mat-ripple mat-tab-label ng-star-inserted" id="mat-tab-label-4-1" tabindex="-1" aria-posinset="2" aria-setsize="3" aria-controls="mat-tab-content-4-1" aria-selected="false" aria-disabled="false"><div class="mat-tab-label-content"><span class="ng-star-inserted">app/heroes/heroes.component.html</span></div></div><div role="tab" mattablabelwrapper="" mat-ripple="" cdkmonitorelementfocus="" class="mat-ripple mat-tab-label ng-star-inserted" id="mat-tab-label-4-2" tabindex="-1" aria-posinset="3" aria-setsize="3" aria-controls="mat-tab-content-4-2" aria-selected="false" aria-disabled="false"><div class="mat-tab-label-content"><span class="ng-star-inserted">app/heroes/heroes.component.css</span></div></div></div><mat-ink-bar class="mat-ink-bar" style="visibility:visible;left:0;width:253px"></mat-ink-bar></div></div><div aria-hidden="true" mat-ripple="" class="mat-ripple mat-tab-header-pagination mat-tab-header-pagination-after mat-elevation-z4"><div class="mat-tab-header-pagination-chevron"></div></div></mat-tab-header><div class="mat-tab-body-wrapper"><mat-tab-body role="tabpanel" class="mat-tab-body ng-tns-c50-19 mat-tab-body-active ng-star-inserted" id="mat-tab-content-4-0" aria-labelledby="mat-tab-label-4-0"><div class="mat-tab-body-content ng-tns-c50-19 ng-trigger ng-trigger-translateTab" style="transform:none"><aio-code class="ng-star-inserted" style=""><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from app/heroes/heroes.component.ts">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="lit">@</span><a href="api/core/Component" class="code-anchor"><span class="lit">Component</span></a><span class="pun">({</span><span class="pln">
  selector</span><span class="pun">:</span><span class="pln"> </span><span class="str">'toh-heroes'</span><span class="pun">,</span><span class="pln">
  </span><a href="api/core/Component#templateUrl" class="code-anchor"><span class="pln">templateUrl</span></a><span class="pun">:</span><span class="pln"> </span><span class="str">'./heroes.component.html'</span><span class="pun">,</span><span class="pln">
  </span><a href="api/core/Component#styleUrls" class="code-anchor"><span class="pln">styleUrls</span></a><span class="pun">:</span><span class="pln">  </span><span class="pun">[</span><span class="str">'./heroes.component.css'</span><span class="pun">]</span><span class="pln">
</span><span class="pun">})</span><span class="pln">
</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">HeroesComponent</span><span class="pln"> </span><span class="kwd">implements</span><span class="pln"> </span><a href="api/core/OnInit" class="code-anchor"><span class="typ">OnInit</span></a><span class="pln"> </span><span class="pun">{</span><span class="pln">
  heroes</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Observable</span><span class="pun">&lt;</span><span class="typ">Hero</span><span class="pun">[]&gt;;</span><span class="pln">
  selectedHero</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Hero</span><span class="pun">;</span><span class="pln">

 </span><span class="kwd">constructor</span><span class="pun">(</span><span class="kwd">private</span><span class="pln"> heroService</span><span class="pun">:</span><span class="pln"> </span><span class="typ">HeroService</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="pun">}</span><span class="pln">

  ngOnInit</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">heroes </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">heroService</span><span class="pun">.</span><span class="pln">getHeroes</span><span class="pun">();</span><span class="pln">
  </span><span class="pun">}</span><span class="pln">
</span><span class="pun">}</span></code>
    </pre></aio-code></div></mat-tab-body><mat-tab-body role="tabpanel" class="mat-tab-body ng-tns-c50-20 ng-star-inserted" id="mat-tab-content-4-1" aria-labelledby="mat-tab-label-4-1"><div class="mat-tab-body-content ng-tns-c50-20 ng-trigger ng-trigger-translateTab" style="transform:translate3d(100%,0,0);min-height:1px"></div></mat-tab-body><mat-tab-body role="tabpanel" class="mat-tab-body ng-tns-c50-21 ng-star-inserted" id="mat-tab-content-4-2" aria-labelledby="mat-tab-label-4-2"><div class="mat-tab-body-content ng-tns-c50-21 ng-trigger ng-trigger-translateTab" style="transform:translate3d(100%,0,0);min-height:1px"></div></mat-tab-body></div></mat-tab-group></mat-card></code-tabs><p translation-result="on"><a href="guide/styleguide#toc">回到顶部</a></p><p translation-origin="off"><a href="guide/styleguide#toc">Back to top</a></p><a id="05-12"></a><h3 id="decorate-input-and-output-properties" translation-result="on">内联输入和输出属性装饰器<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#decorate-input-and-output-properties"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="decorate-input-and-output-properties">Decorate <em>input</em> and <em>output</em> properties<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#decorate-input-and-output-properties"><i class="material-icons">link</i></a></h3><h4 id="style-05-12" translation-result="on">风格 05-12<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-05-12"><i class="material-icons">link</i></a></h4><h4 translation-origin="off" id="style-05-12">Style 05-12<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-05-12"><i class="material-icons">link</i></a></h4><div class="s-rule do"><p translation-result="on"><strong>坚持</strong> 使用 <code>@<a href="api/core/Input" class="code-anchor">Input</a>()</code> 和 <code>@<a href="api/core/Output" class="code-anchor">Output</a>()</code>，而非 <code>@<a href="api/core/Directive" class="code-anchor">Directive</a></code> 和 <code>@<a href="api/core/Component" class="code-anchor">Component</a></code> 装饰器的 <code>inputs</code> 和 <code>outputs</code> 属性:</p><p translation-origin="off"><strong>Do</strong> use the <code>@<a href="api/core/Input" class="code-anchor">Input</a>()</code> and <code>@<a href="api/core/Output" class="code-anchor">Output</a>()</code> class decorators instead of the <code>inputs</code> and <code>outputs</code> properties of the <code>@<a href="api/core/Directive" class="code-anchor">Directive</a></code> and <code>@<a href="api/core/Component" class="code-anchor">Component</a></code> metadata:</p></div><div class="s-rule consider"><p translation-result="on"><strong>坚持</strong>把 <code>@<a href="api/core/Input" class="code-anchor">Input</a>()</code> 或者 <code>@<a href="api/core/Output" class="code-anchor">Output</a>()</code> 放到所装饰的属性的同一行。</p><p translation-origin="off"><strong>Consider</strong> placing <code>@<a href="api/core/Input" class="code-anchor">Input</a>()</code> or <code>@<a href="api/core/Output" class="code-anchor">Output</a>()</code> on the same line as the property it decorates.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong>易于在类里面识别哪些属性是输入属性或输出属性。</p><p translation-origin="off"><strong>Why?</strong> It is easier and more readable to identify which properties in a class are inputs or outputs.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong> 如果需要重命名与 <code>@<a href="api/core/Input" class="code-anchor">Input</a>()</code> 或者 <code>@<a href="api/core/Output" class="code-anchor">Output</a>()</code> 关联的属性或事件名，你可以在一个位置修改。</p><p translation-origin="off"><strong>Why?</strong> If you ever need to rename the property or event name associated with <code>@<a href="api/core/Input" class="code-anchor">Input</a>()</code> or <code>@<a href="api/core/Output" class="code-anchor">Output</a>()</code>, you can modify it in a single place.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong>依附到指令的元数据声明会比较简短，更易于阅读。</p><p translation-origin="off"><strong>Why?</strong> The metadata declaration attached to the directive is shorter and thus more readable.</p></div><div class="s-why-last"><p translation-result="on"><strong>为何？</strong>把装饰器放到同一行可以精简代码，同时更易于识别输入或输出属性。</p><p translation-origin="off"><strong>Why?</strong> Placing the decorator on the same line <em>usually</em> makes for shorter code and still easily identifies the property as an input or output. Put it on the line above when doing so is clearly more readable.</p></div><code-example path="styleguide/src/05-12/app/heroes/shared/hero-button/hero-button.component.avoid.ts" region="example" header="app/heroes/shared/hero-button/hero-button.component.ts" ng-version="9.0.0-rc.11" class="avoidFile"><div style="display:none">/* avoid */ @<a href="api/core/Component" class="code-anchor">Component</a>({ selector: 'toh-hero-button', template: `&lt;button&gt;&lt;/button&gt;`, inputs: [ 'label' ], outputs: [ 'heroChange' ] }) export class HeroButtonComponent { heroChange = new <a href="api/core/EventEmitter" class="code-anchor">EventEmitter</a>&lt;any&gt;(); label: string; }</div><header class="ng-star-inserted">app/heroes/shared/hero-button/hero-button.component.ts</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from app/heroes/shared/hero-button/hero-button.component.ts">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="com">/* avoid */</span><span class="pln">

</span><span class="lit">@</span><a href="api/core/Component" class="code-anchor"><span class="lit">Component</span></a><span class="pun">({</span><span class="pln">
  selector</span><span class="pun">:</span><span class="pln"> </span><span class="str">'toh-hero-button'</span><span class="pun">,</span><span class="pln">
  </span><span class="kwd">template</span><span class="pun">:</span><span class="pln"> </span><span class="str">`&lt;button&gt;&lt;/button&gt;`</span><span class="pun">,</span><span class="pln">
  inputs</span><span class="pun">:</span><span class="pln"> </span><span class="pun">[</span><span class="pln">
    </span><span class="str">'label'</span><span class="pln">
  </span><span class="pun">],</span><span class="pln">
  outputs</span><span class="pun">:</span><span class="pln"> </span><span class="pun">[</span><span class="pln">
    </span><span class="str">'heroChange'</span><span class="pln">
  </span><span class="pun">]</span><span class="pln">
</span><span class="pun">})</span><span class="pln">
</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">HeroButtonComponent</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  heroChange </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><a href="api/core/EventEmitter" class="code-anchor"><span class="typ">EventEmitter</span></a><span class="str">&lt;any&gt;</span><span class="pun">();</span><span class="pln">
  label</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">string</span><span class="pun">;</span><span class="pln">
</span><span class="pun">}</span></code>
    </pre></aio-code></code-example><code-example path="styleguide/src/05-12/app/heroes/shared/hero-button/hero-button.component.ts" region="example" header="app/heroes/shared/hero-button/hero-button.component.ts" ng-version="9.0.0-rc.11"><div style="display:none">@<a href="api/core/Component" class="code-anchor">Component</a>({ selector: 'toh-hero-button', template: `&lt;button&gt;{{label}}&lt;/button&gt;` }) export class HeroButtonComponent { @<a href="api/core/Output" class="code-anchor">Output</a>() heroChange = new <a href="api/core/EventEmitter" class="code-anchor">EventEmitter</a>&lt;any&gt;(); @<a href="api/core/Input" class="code-anchor">Input</a>() label: string; }</div><header class="ng-star-inserted">app/heroes/shared/hero-button/hero-button.component.ts</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from app/heroes/shared/hero-button/hero-button.component.ts">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="lit">@</span><a href="api/core/Component" class="code-anchor"><span class="lit">Component</span></a><span class="pun">({</span><span class="pln">
  selector</span><span class="pun">:</span><span class="pln"> </span><span class="str">'toh-hero-button'</span><span class="pun">,</span><span class="pln">
  </span><span class="kwd">template</span><span class="pun">:</span><span class="pln"> </span><span class="str">`&lt;button&gt;{{label}}&lt;/button&gt;`</span><span class="pln">
</span><span class="pun">})</span><span class="pln">
</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">HeroButtonComponent</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  </span><span class="lit">@</span><a href="api/core/Output" class="code-anchor"><span class="lit">Output</span></a><span class="pun">()</span><span class="pln"> heroChange </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><a href="api/core/EventEmitter" class="code-anchor"><span class="typ">EventEmitter</span></a><span class="str">&lt;any&gt;</span><span class="pun">();</span><span class="pln">
  </span><span class="lit">@</span><a href="api/core/Input" class="code-anchor"><span class="lit">Input</span></a><span class="pun">()</span><span class="pln"> label</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">string</span><span class="pun">;</span><span class="pln">
</span><span class="pun">}</span></code>
    </pre></aio-code></code-example><p translation-result="on"><a href="guide/styleguide#toc">回到顶部</a></p><p translation-origin="off"><a href="guide/styleguide#toc">Back to top</a></p><a id="05-13"></a><h3 id="avoid-aliasing-inputs-and-outputs" translation-result="on">避免为输入和输出属性指定别名<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#avoid-aliasing-inputs-and-outputs"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="avoid-aliasing-inputs-and-outputs">Avoid aliasing <em>inputs</em> and <em>outputs</em><a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#avoid-aliasing-inputs-and-outputs"><i class="material-icons">link</i></a></h3><h4 id="style-05-13" translation-result="on">风格 05-13<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-05-13"><i class="material-icons">link</i></a></h4><h4 translation-origin="off" id="style-05-13">Style 05-13<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-05-13"><i class="material-icons">link</i></a></h4><div class="s-rule avoid"><p translation-result="on"><strong>避免</strong>除非有重要目的，否则不要为输入和输出指定别名。</p><p translation-origin="off"><strong>Avoid</strong> <em>input</em> and <em>output</em> aliases except when it serves an important purpose.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong>同一个属性有两个名字（一个对内一个对外）很容易导致混淆。</p><p translation-origin="off"><strong>Why?</strong> Two names for the same property (one private, one public) is inherently confusing.</p></div><div class="s-why-last"><p translation-result="on"><strong>为何？</strong>如果指令名也同时用作<em>输入</em>属性，而且指令名无法准确描述这个属性的用途时，应该使用别名。</p><p translation-origin="off"><strong>Why?</strong> You should use an alias when the directive name is also an <em>input</em> property, and the directive name doesn't describe the property.</p></div><code-example path="styleguide/src/05-13/app/heroes/shared/hero-button/hero-button.component.avoid.ts" region="example" header="app/heroes/shared/hero-button/hero-button.component.ts" ng-version="9.0.0-rc.11" class="avoidFile"><div style="display:none">/* avoid pointless aliasing */ @<a href="api/core/Component" class="code-anchor">Component</a>({ selector: 'toh-hero-button', template: `&lt;button&gt;{{label}}&lt;/button&gt;` }) export class HeroButtonComponent { // Pointless aliases @<a href="api/core/Output" class="code-anchor">Output</a>('heroChangeEvent') heroChange = new <a href="api/core/EventEmitter" class="code-anchor">EventEmitter</a>&lt;any&gt;(); @<a href="api/core/Input" class="code-anchor">Input</a>('labelAttribute') label: string; }</div><header class="ng-star-inserted">app/heroes/shared/hero-button/hero-button.component.ts</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from app/heroes/shared/hero-button/hero-button.component.ts">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="com">/* avoid pointless aliasing */</span><span class="pln">

</span><span class="lit">@</span><a href="api/core/Component" class="code-anchor"><span class="lit">Component</span></a><span class="pun">({</span><span class="pln">
  selector</span><span class="pun">:</span><span class="pln"> </span><span class="str">'toh-hero-button'</span><span class="pun">,</span><span class="pln">
  </span><span class="kwd">template</span><span class="pun">:</span><span class="pln"> </span><span class="str">`&lt;button&gt;{{label}}&lt;/button&gt;`</span><span class="pln">
</span><span class="pun">})</span><span class="pln">
</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">HeroButtonComponent</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  </span><span class="com">// Pointless aliases</span><span class="pln">
  </span><span class="lit">@</span><a href="api/core/Output" class="code-anchor"><span class="lit">Output</span></a><span class="pun">(</span><span class="str">'heroChangeEvent'</span><span class="pun">)</span><span class="pln"> heroChange </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><a href="api/core/EventEmitter" class="code-anchor"><span class="typ">EventEmitter</span></a><span class="str">&lt;any&gt;</span><span class="pun">();</span><span class="pln">
  </span><span class="lit">@</span><a href="api/core/Input" class="code-anchor"><span class="lit">Input</span></a><span class="pun">(</span><span class="str">'labelAttribute'</span><span class="pun">)</span><span class="pln"> label</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">string</span><span class="pun">;</span><span class="pln">
</span><span class="pun">}</span></code>
    </pre></aio-code></code-example><code-example path="styleguide/src/05-13/app/app.component.avoid.html" header="app/app.component.html" ng-version="9.0.0-rc.11" class="avoidFile"><div style="display:none">&lt;!-- avoid --&gt; &lt;toh-hero-button labelAttribute="OK" (changeEvent)="doSomething()"&gt; &lt;/toh-hero-button&gt;</div><header class="ng-star-inserted">app/app.component.html</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from app/app.component.html">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="com">&lt;!-- avoid --&gt;</span><span class="pln">

</span><span class="tag">&lt;toh-hero-button</span><span class="pln"> </span><span class="atn">labelAttribute</span><span class="pun">=</span><span class="atv">"OK"</span><span class="pln"> (</span><span class="atn">changeEvent</span><span class="pln">)</span><span class="pun">=</span><span class="atv">"doSomething()"</span><span class="tag">&gt;</span><span class="pln">
</span><span class="tag">&lt;/toh-hero-button&gt;</span></code>
    </pre></aio-code></code-example><code-tabs ng-version="9.0.0-rc.11"><div style="display:none"><code-pane header="app/heroes/shared/hero-button/hero-button.component.ts" path="styleguide/src/05-13/app/heroes/shared/hero-button/hero-button.component.ts" region="example">@<a href="api/core/Component" class="code-anchor">Component</a>({ selector: 'toh-hero-button', template: `&lt;button&gt;{{label}}&lt;/button&gt;` }) export class HeroButtonComponent { // No aliases @<a href="api/core/Output" class="code-anchor">Output</a>() heroChange = new <a href="api/core/EventEmitter" class="code-anchor">EventEmitter</a>&lt;any&gt;(); @<a href="api/core/Input" class="code-anchor">Input</a>() label: string; }</code-pane><code-pane header="app/heroes/shared/hero-button/hero-highlight.directive.ts" path="styleguide/src/05-13/app/heroes/shared/hero-highlight.directive.ts">import { <a href="api/core/Directive" class="code-anchor">Directive</a>, <a href="api/core/ElementRef" class="code-anchor">ElementRef</a>, <a href="api/core/Input" class="code-anchor">Input</a>, <a href="api/core/OnChanges" class="code-anchor">OnChanges</a> } from '@angular/core'; @<a href="api/core/Directive" class="code-anchor">Directive</a>({ selector: '[heroHighlight]' }) export class HeroHighlightDirective implements <a href="api/core/OnChanges" class="code-anchor">OnChanges</a> { // Aliased because `color` is a better property name than `heroHighlight` @<a href="api/core/Input" class="code-anchor">Input</a>('heroHighlight') color: string; constructor(private el: <a href="api/core/ElementRef" class="code-anchor">ElementRef</a>) {} ngOnChanges() { this.el.nativeElement.style.backgroundColor = this.color || 'yellow'; } }</code-pane><code-pane header="app/app.component.html" path="styleguide/src/05-13/app/app.component.html">&lt;toh-hero-button label="OK" (change)="doSomething()"&gt; &lt;/toh-hero-button&gt; &lt;!-- `heroHighlight` is both the directive name and the data-bound aliased property name --&gt; &lt;h3 heroHighlight="skyblue"&gt;The Great Bombasto&lt;/h3&gt;</code-pane></div><mat-card class="mat-card"><mat-tab-group class="mat-tab-group code-tab-group mat-primary"><mat-tab-header class="mat-tab-header mat-tab-header-pagination-controls-enabled"><div aria-hidden="true" mat-ripple="" class="mat-ripple mat-tab-header-pagination mat-tab-header-pagination-before mat-elevation-z4 mat-tab-header-pagination-disabled"><div class="mat-tab-header-pagination-chevron"></div></div><div class="mat-tab-label-container"><div role="tablist" class="mat-tab-list" style="transform:translateX(0)"><div class="mat-tab-labels"><div role="tab" mattablabelwrapper="" mat-ripple="" cdkmonitorelementfocus="" class="mat-ripple mat-tab-label mat-tab-label-active ng-star-inserted" id="mat-tab-label-5-0" tabindex="0" aria-posinset="1" aria-setsize="3" aria-controls="mat-tab-content-5-0" aria-selected="true" aria-disabled="false"><div class="mat-tab-label-content"><span class="ng-star-inserted">app/heroes/shared/hero-button/hero-button.component.ts</span></div></div><div role="tab" mattablabelwrapper="" mat-ripple="" cdkmonitorelementfocus="" class="mat-ripple mat-tab-label ng-star-inserted" id="mat-tab-label-5-1" tabindex="-1" aria-posinset="2" aria-setsize="3" aria-controls="mat-tab-content-5-1" aria-selected="false" aria-disabled="false"><div class="mat-tab-label-content"><span class="ng-star-inserted">app/heroes/shared/hero-button/hero-highlight.directive.ts</span></div></div><div role="tab" mattablabelwrapper="" mat-ripple="" cdkmonitorelementfocus="" class="mat-ripple mat-tab-label ng-star-inserted" id="mat-tab-label-5-2" tabindex="-1" aria-posinset="3" aria-setsize="3" aria-controls="mat-tab-content-5-2" aria-selected="false" aria-disabled="false"><div class="mat-tab-label-content"><span class="ng-star-inserted">app/app.component.html</span></div></div></div><mat-ink-bar class="mat-ink-bar" style="visibility:visible;left:0;width:404px"></mat-ink-bar></div></div><div aria-hidden="true" mat-ripple="" class="mat-ripple mat-tab-header-pagination mat-tab-header-pagination-after mat-elevation-z4"><div class="mat-tab-header-pagination-chevron"></div></div></mat-tab-header><div class="mat-tab-body-wrapper"><mat-tab-body role="tabpanel" class="mat-tab-body ng-tns-c50-22 mat-tab-body-active ng-star-inserted" id="mat-tab-content-5-0" aria-labelledby="mat-tab-label-5-0"><div class="mat-tab-body-content ng-tns-c50-22 ng-trigger ng-trigger-translateTab" style="transform:none"><aio-code class="ng-star-inserted" style=""><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from app/heroes/shared/hero-button/hero-button.component.ts">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="lit">@</span><a href="api/core/Component" class="code-anchor"><span class="lit">Component</span></a><span class="pun">({</span><span class="pln">
  selector</span><span class="pun">:</span><span class="pln"> </span><span class="str">'toh-hero-button'</span><span class="pun">,</span><span class="pln">
  </span><span class="kwd">template</span><span class="pun">:</span><span class="pln"> </span><span class="str">`&lt;button&gt;{{label}}&lt;/button&gt;`</span><span class="pln">
</span><span class="pun">})</span><span class="pln">
</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">HeroButtonComponent</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  </span><span class="com">// No aliases</span><span class="pln">
  </span><span class="lit">@</span><a href="api/core/Output" class="code-anchor"><span class="lit">Output</span></a><span class="pun">()</span><span class="pln"> heroChange </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><a href="api/core/EventEmitter" class="code-anchor"><span class="typ">EventEmitter</span></a><span class="str">&lt;any&gt;</span><span class="pun">();</span><span class="pln">
  </span><span class="lit">@</span><a href="api/core/Input" class="code-anchor"><span class="lit">Input</span></a><span class="pun">()</span><span class="pln"> label</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">string</span><span class="pun">;</span><span class="pln">
</span><span class="pun">}</span></code>
    </pre></aio-code></div></mat-tab-body><mat-tab-body role="tabpanel" class="mat-tab-body ng-tns-c50-23 ng-star-inserted" id="mat-tab-content-5-1" aria-labelledby="mat-tab-label-5-1"><div class="mat-tab-body-content ng-tns-c50-23 ng-trigger ng-trigger-translateTab" style="transform:translate3d(100%,0,0);min-height:1px"></div></mat-tab-body><mat-tab-body role="tabpanel" class="mat-tab-body ng-tns-c50-24 ng-star-inserted" id="mat-tab-content-5-2" aria-labelledby="mat-tab-label-5-2"><div class="mat-tab-body-content ng-tns-c50-24 ng-trigger ng-trigger-translateTab" style="transform:translate3d(100%,0,0);min-height:1px"></div></mat-tab-body></div></mat-tab-group></mat-card></code-tabs><p translation-result="on"><a href="guide/styleguide#toc">回到顶部</a></p><p translation-origin="off"><a href="guide/styleguide#toc">Back to top</a></p><a id="05-14"></a><h3 id="member-sequence" translation-result="on">成员顺序<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#member-sequence"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="member-sequence">Member sequence<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#member-sequence"><i class="material-icons">link</i></a></h3><h4 id="style-05-14" translation-result="on">风格 05-14<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-05-14"><i class="material-icons">link</i></a></h4><h4 translation-origin="off" id="style-05-14">Style 05-14<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-05-14"><i class="material-icons">link</i></a></h4><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>把属性成员放在前面，方法成员放在后面。</p><p translation-origin="off"><strong>Do</strong> place properties up top followed by methods.</p></div><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>先放公共成员，再放私有成员，并按照字母顺序排列。</p><p translation-origin="off"><strong>Do</strong> place private members after public members, alphabetized.</p></div><div class="s-why-last"><p translation-result="on"><strong>为何？</strong>把类的成员按照统一的顺序排列，易于阅读，能立即识别出组件的哪个成员服务于何种目的。</p><p translation-origin="off"><strong>Why?</strong> Placing members in a consistent sequence makes it easy to read and helps instantly identify which members of the component serve which purpose.</p></div><code-example path="styleguide/src/05-14/app/shared/toast/toast.component.avoid.ts" region="example" header="app/shared/toast/toast.component.ts" ng-version="9.0.0-rc.11" class="avoidFile"><div style="display:none">/* avoid */ export class ToastComponent implements <a href="api/core/OnInit" class="code-anchor">OnInit</a> { private defaults = { title: '', <a href="api/common/http/HttpErrorResponse#message" class="code-anchor">message</a>: 'May the Force be with you' }; <a href="api/common/http/HttpErrorResponse#message" class="code-anchor">message</a>: string; title: string; private toastElement: any; ngOnInit() { this.toastElement = document.getElementById('toh-toast'); } // private methods private hide() { this.toastElement.style.opacity = 0; window.setTimeout(() =&gt; this.toastElement.style.zIndex = 0, 400); } activate(<a href="api/common/http/HttpErrorResponse#message" class="code-anchor">message</a> = this.defaults.message, title = this.defaults.title) { this.title = title; this.message = <a href="api/common/http/HttpErrorResponse#message" class="code-anchor">message</a>; this.show(); } private show() { console.log(this.message); this.toastElement.style.opacity = 1; this.toastElement.style.zIndex = 9999; window.setTimeout(() =&gt; this.hide(), 2500); } }</div><header class="ng-star-inserted">app/shared/toast/toast.component.ts</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from app/shared/toast/toast.component.ts">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="com">/* avoid */</span><span class="pln">

</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">ToastComponent</span><span class="pln"> </span><span class="kwd">implements</span><span class="pln"> </span><a href="api/core/OnInit" class="code-anchor"><span class="typ">OnInit</span></a><span class="pln"> </span><span class="pun">{</span><span class="pln">

  </span><span class="kwd">private</span><span class="pln"> defaults </span><span class="pun">=</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    title</span><span class="pun">:</span><span class="pln"> </span><span class="str">''</span><span class="pun">,</span><span class="pln">
    </span><a href="api/common/http/HttpErrorResponse#message" class="code-anchor"><span class="pln">message</span></a><span class="pun">:</span><span class="pln"> </span><span class="str">'May the Force be with you'</span><span class="pln">
  </span><span class="pun">};</span><span class="pln">
  </span><a href="api/common/http/HttpErrorResponse#message" class="code-anchor"><span class="pln">message</span></a><span class="pun">:</span><span class="pln"> </span><span class="kwd">string</span><span class="pun">;</span><span class="pln">
  title</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">string</span><span class="pun">;</span><span class="pln">
  </span><span class="kwd">private</span><span class="pln"> toastElement</span><span class="pun">:</span><span class="pln"> any</span><span class="pun">;</span><span class="pln">

  ngOnInit</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">toastElement </span><span class="pun">=</span><span class="pln"> document</span><span class="pun">.</span><span class="pln">getElementById</span><span class="pun">(</span><span class="str">'toh-toast'</span><span class="pun">);</span><span class="pln">
  </span><span class="pun">}</span><span class="pln">

  </span><span class="com">// private methods</span><span class="pln">
  </span><span class="kwd">private</span><span class="pln"> hide</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">toastElement</span><span class="pun">.</span><span class="pln">style</span><span class="pun">.</span><span class="pln">opacity </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pln">
    window</span><span class="pun">.</span><span class="pln">setTimeout</span><span class="pun">(()</span><span class="pln"> </span><span class="pun">=&gt;</span><span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">toastElement</span><span class="pun">.</span><span class="pln">style</span><span class="pun">.</span><span class="pln">zIndex </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">,</span><span class="pln"> </span><span class="lit">400</span><span class="pun">);</span><span class="pln">
  </span><span class="pun">}</span><span class="pln">

  activate</span><span class="pun">(</span><a href="api/common/http/HttpErrorResponse#message" class="code-anchor"><span class="pln">message</span></a><span class="pln"> </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">defaults</span><span class="pun">.</span><span class="pln">message</span><span class="pun">,</span><span class="pln"> title </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">defaults</span><span class="pun">.</span><span class="pln">title</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">title </span><span class="pun">=</span><span class="pln"> title</span><span class="pun">;</span><span class="pln">
    </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">message </span><span class="pun">=</span><span class="pln"> </span><a href="api/common/http/HttpErrorResponse#message" class="code-anchor"><span class="pln">message</span></a><span class="pun">;</span><span class="pln">
    </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">show</span><span class="pun">();</span><span class="pln">
  </span><span class="pun">}</span><span class="pln">

  </span><span class="kwd">private</span><span class="pln"> show</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">message</span><span class="pun">);</span><span class="pln">
    </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">toastElement</span><span class="pun">.</span><span class="pln">style</span><span class="pun">.</span><span class="pln">opacity </span><span class="pun">=</span><span class="pln"> </span><span class="lit">1</span><span class="pun">;</span><span class="pln">
    </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">toastElement</span><span class="pun">.</span><span class="pln">style</span><span class="pun">.</span><span class="pln">zIndex </span><span class="pun">=</span><span class="pln"> </span><span class="lit">9999</span><span class="pun">;</span><span class="pln">

    window</span><span class="pun">.</span><span class="pln">setTimeout</span><span class="pun">(()</span><span class="pln"> </span><span class="pun">=&gt;</span><span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">hide</span><span class="pun">(),</span><span class="pln"> </span><span class="lit">2500</span><span class="pun">);</span><span class="pln">
  </span><span class="pun">}</span><span class="pln">
</span><span class="pun">}</span></code>
    </pre></aio-code></code-example><code-example path="styleguide/src/05-14/app/shared/toast/toast.component.ts" region="example" header="app/shared/toast/toast.component.ts" ng-version="9.0.0-rc.11"><div style="display:none">export class ToastComponent implements <a href="api/core/OnInit" class="code-anchor">OnInit</a> { // public <a href="api/core/DebugElement#properties" class="code-anchor">properties</a> <a href="api/common/http/HttpErrorResponse#message" class="code-anchor">message</a>: string; title: string; // private fields private defaults = { title: '', <a href="api/common/http/HttpErrorResponse#message" class="code-anchor">message</a>: 'May the Force be with you' }; private toastElement: any; // public methods activate(<a href="api/common/http/HttpErrorResponse#message" class="code-anchor">message</a> = this.defaults.message, title = this.defaults.title) { this.title = title; this.message = <a href="api/common/http/HttpErrorResponse#message" class="code-anchor">message</a>; this.show(); } ngOnInit() { this.toastElement = document.getElementById('toh-toast'); } // private methods private hide() { this.toastElement.style.opacity = 0; window.setTimeout(() =&gt; this.toastElement.style.zIndex = 0, 400); } private show() { console.log(this.message); this.toastElement.style.opacity = 1; this.toastElement.style.zIndex = 9999; window.setTimeout(() =&gt; this.hide(), 2500); } }</div><header class="ng-star-inserted">app/shared/toast/toast.component.ts</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from app/shared/toast/toast.component.ts">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">ToastComponent</span><span class="pln"> </span><span class="kwd">implements</span><span class="pln"> </span><a href="api/core/OnInit" class="code-anchor"><span class="typ">OnInit</span></a><span class="pln"> </span><span class="pun">{</span><span class="pln">
  </span><span class="com">// public </span><a href="api/core/DebugElement#properties" class="code-anchor"><span class="com">properties</span></a><span class="pln">
  </span><a href="api/common/http/HttpErrorResponse#message" class="code-anchor"><span class="pln">message</span></a><span class="pun">:</span><span class="pln"> </span><span class="kwd">string</span><span class="pun">;</span><span class="pln">
  title</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">string</span><span class="pun">;</span><span class="pln">

  </span><span class="com">// private fields</span><span class="pln">
  </span><span class="kwd">private</span><span class="pln"> defaults </span><span class="pun">=</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    title</span><span class="pun">:</span><span class="pln"> </span><span class="str">''</span><span class="pun">,</span><span class="pln">
    </span><a href="api/common/http/HttpErrorResponse#message" class="code-anchor"><span class="pln">message</span></a><span class="pun">:</span><span class="pln"> </span><span class="str">'May the Force be with you'</span><span class="pln">
  </span><span class="pun">};</span><span class="pln">
  </span><span class="kwd">private</span><span class="pln"> toastElement</span><span class="pun">:</span><span class="pln"> any</span><span class="pun">;</span><span class="pln">

  </span><span class="com">// public methods</span><span class="pln">
  activate</span><span class="pun">(</span><a href="api/common/http/HttpErrorResponse#message" class="code-anchor"><span class="pln">message</span></a><span class="pln"> </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">defaults</span><span class="pun">.</span><span class="pln">message</span><span class="pun">,</span><span class="pln"> title </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">defaults</span><span class="pun">.</span><span class="pln">title</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">title </span><span class="pun">=</span><span class="pln"> title</span><span class="pun">;</span><span class="pln">
    </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">message </span><span class="pun">=</span><span class="pln"> </span><a href="api/common/http/HttpErrorResponse#message" class="code-anchor"><span class="pln">message</span></a><span class="pun">;</span><span class="pln">
    </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">show</span><span class="pun">();</span><span class="pln">
  </span><span class="pun">}</span><span class="pln">

  ngOnInit</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">toastElement </span><span class="pun">=</span><span class="pln"> document</span><span class="pun">.</span><span class="pln">getElementById</span><span class="pun">(</span><span class="str">'toh-toast'</span><span class="pun">);</span><span class="pln">
  </span><span class="pun">}</span><span class="pln">

  </span><span class="com">// private methods</span><span class="pln">
  </span><span class="kwd">private</span><span class="pln"> hide</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">toastElement</span><span class="pun">.</span><span class="pln">style</span><span class="pun">.</span><span class="pln">opacity </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pln">
    window</span><span class="pun">.</span><span class="pln">setTimeout</span><span class="pun">(()</span><span class="pln"> </span><span class="pun">=&gt;</span><span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">toastElement</span><span class="pun">.</span><span class="pln">style</span><span class="pun">.</span><span class="pln">zIndex </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">,</span><span class="pln"> </span><span class="lit">400</span><span class="pun">);</span><span class="pln">
  </span><span class="pun">}</span><span class="pln">

  </span><span class="kwd">private</span><span class="pln"> show</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">message</span><span class="pun">);</span><span class="pln">
    </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">toastElement</span><span class="pun">.</span><span class="pln">style</span><span class="pun">.</span><span class="pln">opacity </span><span class="pun">=</span><span class="pln"> </span><span class="lit">1</span><span class="pun">;</span><span class="pln">
    </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">toastElement</span><span class="pun">.</span><span class="pln">style</span><span class="pun">.</span><span class="pln">zIndex </span><span class="pun">=</span><span class="pln"> </span><span class="lit">9999</span><span class="pun">;</span><span class="pln">
    window</span><span class="pun">.</span><span class="pln">setTimeout</span><span class="pun">(()</span><span class="pln"> </span><span class="pun">=&gt;</span><span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">hide</span><span class="pun">(),</span><span class="pln"> </span><span class="lit">2500</span><span class="pun">);</span><span class="pln">
  </span><span class="pun">}</span><span class="pln">
</span><span class="pun">}</span></code>
    </pre></aio-code></code-example><p translation-result="on"><a href="guide/styleguide#toc">回到顶部</a></p><p translation-origin="off"><a href="guide/styleguide#toc">Back to top</a></p><a id="05-15"></a><h3 id="delegate-complex-component-logic-to-services" translation-result="on">把逻辑放到服务里<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#delegate-complex-component-logic-to-services"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="delegate-complex-component-logic-to-services">Delegate complex component logic to services<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#delegate-complex-component-logic-to-services"><i class="material-icons">link</i></a></h3><h4 id="style-05-15" translation-result="on">风格 05-15<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-05-15"><i class="material-icons">link</i></a></h4><h4 translation-origin="off" id="style-05-15">Style 05-15<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-05-15"><i class="material-icons">link</i></a></h4><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>在组件中只包含与视图相关的逻辑。所有其它逻辑都应该放到服务中。</p><p translation-origin="off"><strong>Do</strong> limit logic in a component to only that required for the view. All other logic should be delegated to services.</p></div><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>把可重用的逻辑放到服务中，保持组件简单，聚焦于它们预期目的。</p><p translation-origin="off"><strong>Do</strong> move reusable logic to services and keep components simple and focused on their intended purpose.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong>当逻辑被放置到服务里，并以函数的形式暴露时，可以被多个组件重复使用。</p><p translation-origin="off"><strong>Why?</strong> Logic may be reused by multiple components when placed within a service and exposed via a function.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong>在单元测试时，服务里的逻辑更容易被隔离。当组件中调用逻辑时，也很容易被模拟。</p><p translation-origin="off"><strong>Why?</strong> Logic in a service can more easily be isolated in a unit test, while the calling logic in the component can be easily mocked.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong>从组件移除依赖并隐藏实现细节。</p><p translation-origin="off"><strong>Why?</strong> Removes dependencies and hides implementation details from the component.</p></div><div class="s-why-last"><p translation-result="on"><strong>为何？</strong>保持组件苗条、精简和聚焦。</p><p translation-origin="off"><strong>Why?</strong> Keeps the component slim, trim, and focused.</p></div><code-example path="styleguide/src/05-15/app/heroes/hero-list/hero-list.component.avoid.ts" header="app/heroes/hero-list/hero-list.component.ts" ng-version="9.0.0-rc.11" class="avoidFile"><div style="display:none">/* avoid */ import { <a href="api/core/OnInit" class="code-anchor">OnInit</a> } from '@angular/core'; import { <a href="api/common/http/HttpClient" class="code-anchor">HttpClient</a> } from '@angular/common/<a href="api/common/http" class="code-anchor">http</a>'; import { Observable } from 'rxjs'; import { catchError, finalize } from 'rxjs/operators'; import { Hero } from '../shared/hero.model'; const heroesUrl = 'http://angular.io'; export class HeroListComponent implements <a href="api/core/OnInit" class="code-anchor">OnInit</a> { heroes: Hero[]; constructor(private <a href="api/common/http" class="code-anchor">http</a>: <a href="api/common/http/HttpClient" class="code-anchor">HttpClient</a>) {} getHeroes() { this.heroes = []; this.http.get(heroesUrl).pipe( catchError(this.catchBadResponse), finalize(() =&gt; this.hideSpinner()) ).subscribe((heroes: Hero[]) =&gt; this.heroes = heroes); } ngOnInit() { this.getHeroes(); } private catchBadResponse(err: any, source: Observable&lt;any&gt;) { // <a href="api/animations/browser/testing/MockAnimationDriver#log" class="code-anchor">log</a> and handle the exception return new Observable(); } private hideSpinner() { // hide the spinner } }</div><header class="ng-star-inserted">app/heroes/hero-list/hero-list.component.ts</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from app/heroes/hero-list/hero-list.component.ts">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="com">/* avoid */</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><a href="api/core/OnInit" class="code-anchor"><span class="typ">OnInit</span></a><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">from</span><span class="pln"> </span><span class="str">'@angular/core'</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">import</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><a href="api/common/http/HttpClient" class="code-anchor"><span class="typ">HttpClient</span></a><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">from</span><span class="pln"> </span><span class="str">'@angular/common/</span><a href="api/common/http" class="code-anchor"><span class="str">http</span></a><span class="str">'</span><span class="pun">;</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="typ">Observable</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">from</span><span class="pln"> </span><span class="str">'rxjs'</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">import</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> catchError</span><span class="pun">,</span><span class="pln"> finalize </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">from</span><span class="pln"> </span><span class="str">'rxjs/operators'</span><span class="pun">;</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="typ">Hero</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">from</span><span class="pln"> </span><span class="str">'../shared/hero.model'</span><span class="pun">;</span><span class="pln">

</span><span class="kwd">const</span><span class="pln"> heroesUrl </span><span class="pun">=</span><span class="pln"> </span><span class="str">'http://angular.io'</span><span class="pun">;</span><span class="pln">

</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">HeroListComponent</span><span class="pln"> </span><span class="kwd">implements</span><span class="pln"> </span><a href="api/core/OnInit" class="code-anchor"><span class="typ">OnInit</span></a><span class="pln"> </span><span class="pun">{</span><span class="pln">
  heroes</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Hero</span><span class="pun">[];</span><span class="pln">
  </span><span class="kwd">constructor</span><span class="pun">(</span><span class="kwd">private</span><span class="pln"> </span><a href="api/common/http" class="code-anchor"><span class="pln">http</span></a><span class="pun">:</span><span class="pln"> </span><a href="api/common/http/HttpClient" class="code-anchor"><span class="typ">HttpClient</span></a><span class="pun">)</span><span class="pln"> </span><span class="pun">{}</span><span class="pln">
  getHeroes</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">heroes </span><span class="pun">=</span><span class="pln"> </span><span class="pun">[];</span><span class="pln">
    </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">http</span><span class="pun">.</span><span class="kwd">get</span><span class="pun">(</span><span class="pln">heroesUrl</span><span class="pun">).</span><span class="pln">pipe</span><span class="pun">(</span><span class="pln">
      catchError</span><span class="pun">(</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">catchBadResponse</span><span class="pun">),</span><span class="pln">
      finalize</span><span class="pun">(()</span><span class="pln"> </span><span class="pun">=&gt;</span><span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">hideSpinner</span><span class="pun">())</span><span class="pln">
    </span><span class="pun">).</span><span class="pln">subscribe</span><span class="pun">((</span><span class="pln">heroes</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Hero</span><span class="pun">[])</span><span class="pln"> </span><span class="pun">=&gt;</span><span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">heroes </span><span class="pun">=</span><span class="pln"> heroes</span><span class="pun">);</span><span class="pln">
  </span><span class="pun">}</span><span class="pln">
  ngOnInit</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">getHeroes</span><span class="pun">();</span><span class="pln">
  </span><span class="pun">}</span><span class="pln">

  </span><span class="kwd">private</span><span class="pln"> catchBadResponse</span><span class="pun">(</span><span class="pln">err</span><span class="pun">:</span><span class="pln"> any</span><span class="pun">,</span><span class="pln"> source</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Observable</span><span class="str">&lt;any&gt;</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="com">// </span><a href="api/animations/browser/testing/MockAnimationDriver#log" class="code-anchor"><span class="com">log</span></a><span class="com"> and handle the exception</span><span class="pln">
    </span><span class="kwd">return</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">Observable</span><span class="pun">();</span><span class="pln">
  </span><span class="pun">}</span><span class="pln">

  </span><span class="kwd">private</span><span class="pln"> hideSpinner</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="com">// hide the spinner</span><span class="pln">
  </span><span class="pun">}</span><span class="pln">
</span><span class="pun">}</span></code>
    </pre></aio-code></code-example><code-example path="styleguide/src/05-15/app/heroes/hero-list/hero-list.component.ts" region="example" header="app/heroes/hero-list/hero-list.component.ts" ng-version="9.0.0-rc.11"><div style="display:none">import { <a href="api/core/Component" class="code-anchor">Component</a>, <a href="api/core/OnInit" class="code-anchor">OnInit</a> } from '@angular/core'; import { Hero, HeroService } from '../shared'; @<a href="api/core/Component" class="code-anchor">Component</a>({ selector: 'toh-hero-list', template: `...` }) export class HeroListComponent implements <a href="api/core/OnInit" class="code-anchor">OnInit</a> { heroes: Hero[]; constructor(private heroService: HeroService) {} getHeroes() { this.heroes = []; this.heroService.getHeroes() .subscribe(heroes =&gt; this.heroes = heroes); } ngOnInit() { this.getHeroes(); } }</div><header class="ng-star-inserted">app/heroes/hero-list/hero-list.component.ts</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from app/heroes/hero-list/hero-list.component.ts">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="kwd">import</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><a href="api/core/Component" class="code-anchor"><span class="typ">Component</span></a><span class="pun">,</span><span class="pln"> </span><a href="api/core/OnInit" class="code-anchor"><span class="typ">OnInit</span></a><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">from</span><span class="pln"> </span><span class="str">'@angular/core'</span><span class="pun">;</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="typ">Hero</span><span class="pun">,</span><span class="pln"> </span><span class="typ">HeroService</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">from</span><span class="pln"> </span><span class="str">'../shared'</span><span class="pun">;</span><span class="pln">

</span><span class="lit">@</span><a href="api/core/Component" class="code-anchor"><span class="lit">Component</span></a><span class="pun">({</span><span class="pln">
  selector</span><span class="pun">:</span><span class="pln"> </span><span class="str">'toh-hero-list'</span><span class="pun">,</span><span class="pln">
  </span><span class="kwd">template</span><span class="pun">:</span><span class="pln"> </span><span class="str">`...`</span><span class="pln">
</span><span class="pun">})</span><span class="pln">
</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">HeroListComponent</span><span class="pln"> </span><span class="kwd">implements</span><span class="pln"> </span><a href="api/core/OnInit" class="code-anchor"><span class="typ">OnInit</span></a><span class="pln"> </span><span class="pun">{</span><span class="pln">
  heroes</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Hero</span><span class="pun">[];</span><span class="pln">
  </span><span class="kwd">constructor</span><span class="pun">(</span><span class="kwd">private</span><span class="pln"> heroService</span><span class="pun">:</span><span class="pln"> </span><span class="typ">HeroService</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{}</span><span class="pln">
  getHeroes</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">heroes </span><span class="pun">=</span><span class="pln"> </span><span class="pun">[];</span><span class="pln">
    </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">heroService</span><span class="pun">.</span><span class="pln">getHeroes</span><span class="pun">()</span><span class="pln">
      </span><span class="pun">.</span><span class="pln">subscribe</span><span class="pun">(</span><span class="pln">heroes </span><span class="pun">=&gt;</span><span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">heroes </span><span class="pun">=</span><span class="pln"> heroes</span><span class="pun">);</span><span class="pln">
  </span><span class="pun">}</span><span class="pln">
  ngOnInit</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">getHeroes</span><span class="pun">();</span><span class="pln">
  </span><span class="pun">}</span><span class="pln">
</span><span class="pun">}</span></code>
    </pre></aio-code></code-example><p translation-result="on"><a href="guide/styleguide#toc">回到顶部</a></p><p translation-origin="off"><a href="guide/styleguide#toc">Back to top</a></p><a id="05-16"></a><h3 id="dont-prefix-output-properties" translation-result="on">不要给输出属性加前缀<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#dont-prefix-output-properties"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="dont-prefix-output-properties">Don't prefix <em>output</em> properties<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#dont-prefix-output-properties"><i class="material-icons">link</i></a></h3><h4 id="style-05-16" translation-result="on">风格 05-16<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-05-16"><i class="material-icons">link</i></a></h4><h4 translation-origin="off" id="style-05-16">Style 05-16<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-05-16"><i class="material-icons">link</i></a></h4><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>命名事件时，不要带前缀 <code>on</code>。</p><p translation-origin="off"><strong>Do</strong> name events without the prefix <code>on</code>.</p></div><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>把事件处理器方法命名为 <code>on</code> 前缀之后紧跟着事件名。</p><p translation-origin="off"><strong>Do</strong> name event handler methods with the prefix <code>on</code> followed by the event name.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong>与内置事件命名一致，例如按钮点击。</p><p translation-origin="off"><strong>Why?</strong> This is consistent with built-in events such as button clicks.</p></div><div class="s-why-last"><p translation-result="on"><strong>为何？</strong>Angular 允许<a href="guide/template-syntax#binding-syntax">另一种备选语法</a> <code>on-*</code>。如果事件的名字本身带有前缀 <code>on</code>，那么绑定的表达式可能是 <code>on-onEvent</code>。</p><p translation-origin="off"><strong>Why?</strong> Angular allows for an <a href="guide/template-syntax#binding-syntax">alternative syntax</a> <code>on-*</code>. If the event itself was prefixed with <code>on</code> this would result in an <code>on-onEvent</code> binding expression.</p></div><code-example path="styleguide/src/05-16/app/heroes/hero.component.avoid.ts" region="example" header="app/heroes/hero.component.ts" ng-version="9.0.0-rc.11" class="avoidFile"><div style="display:none">/* avoid */ @<a href="api/core/Component" class="code-anchor">Component</a>({ selector: 'toh-hero', template: `...` }) export class HeroComponent { @<a href="api/core/Output" class="code-anchor">Output</a>() onSavedTheDay = new <a href="api/core/EventEmitter" class="code-anchor">EventEmitter</a>&lt;boolean&gt;(); }</div><header class="ng-star-inserted">app/heroes/hero.component.ts</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from app/heroes/hero.component.ts">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="com">/* avoid */</span><span class="pln">

</span><span class="lit">@</span><a href="api/core/Component" class="code-anchor"><span class="lit">Component</span></a><span class="pun">({</span><span class="pln">
  selector</span><span class="pun">:</span><span class="pln"> </span><span class="str">'toh-hero'</span><span class="pun">,</span><span class="pln">
  </span><span class="kwd">template</span><span class="pun">:</span><span class="pln"> </span><span class="str">`...`</span><span class="pln">
</span><span class="pun">})</span><span class="pln">
</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">HeroComponent</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  </span><span class="lit">@</span><a href="api/core/Output" class="code-anchor"><span class="lit">Output</span></a><span class="pun">()</span><span class="pln"> onSavedTheDay </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><a href="api/core/EventEmitter" class="code-anchor"><span class="typ">EventEmitter</span></a><span class="str">&lt;boolean&gt;</span><span class="pun">();</span><span class="pln">
</span><span class="pun">}</span></code>
    </pre></aio-code></code-example><code-example path="styleguide/src/05-16/app/app.component.avoid.html" header="app/app.component.html" ng-version="9.0.0-rc.11" class="avoidFile"><div style="display:none">&lt;!-- avoid --&gt; &lt;toh-hero (onSavedTheDay)="onSavedTheDay($event)"&gt;&lt;/toh-hero&gt;</div><header class="ng-star-inserted">app/app.component.html</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from app/app.component.html">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="com">&lt;!-- avoid --&gt;</span><span class="pln">

</span><span class="tag">&lt;toh-hero</span><span class="pln"> (</span><span class="atn">onSavedTheDay</span><span class="pln">)</span><span class="pun">=</span><span class="atv">"onSavedTheDay($event)"</span><span class="tag">&gt;&lt;/toh-hero&gt;</span></code>
    </pre></aio-code></code-example><code-tabs ng-version="9.0.0-rc.11"><div style="display:none"><code-pane header="app/heroes/hero.component.ts" path="styleguide/src/05-16/app/heroes/hero.component.ts" region="example">export class HeroComponent { @<a href="api/core/Output" class="code-anchor">Output</a>() savedTheDay = new <a href="api/core/EventEmitter" class="code-anchor">EventEmitter</a>&lt;boolean&gt;(); }</code-pane><code-pane header="app/app.component.html" path="styleguide/src/05-16/app/app.component.html">&lt;toh-hero (savedTheDay)="onSavedTheDay($event)"&gt;&lt;/toh-hero&gt;</code-pane></div><mat-card class="mat-card"><mat-tab-group class="mat-tab-group code-tab-group mat-primary"><mat-tab-header class="mat-tab-header"><div aria-hidden="true" mat-ripple="" class="mat-ripple mat-tab-header-pagination mat-tab-header-pagination-before mat-elevation-z4 mat-tab-header-pagination-disabled"><div class="mat-tab-header-pagination-chevron"></div></div><div class="mat-tab-label-container"><div role="tablist" class="mat-tab-list" style="transform:translateX(0)"><div class="mat-tab-labels"><div role="tab" mattablabelwrapper="" mat-ripple="" cdkmonitorelementfocus="" class="mat-ripple mat-tab-label mat-tab-label-active ng-star-inserted" id="mat-tab-label-6-0" tabindex="0" aria-posinset="1" aria-setsize="2" aria-controls="mat-tab-content-6-0" aria-selected="true" aria-disabled="false"><div class="mat-tab-label-content"><span class="ng-star-inserted">app/heroes/hero.component.ts</span></div></div><div role="tab" mattablabelwrapper="" mat-ripple="" cdkmonitorelementfocus="" class="mat-ripple mat-tab-label ng-star-inserted" id="mat-tab-label-6-1" tabindex="-1" aria-posinset="2" aria-setsize="2" aria-controls="mat-tab-content-6-1" aria-selected="false" aria-disabled="false"><div class="mat-tab-label-content"><span class="ng-star-inserted">app/app.component.html</span></div></div></div><mat-ink-bar class="mat-ink-bar" style="visibility:visible;left:0;width:238px"></mat-ink-bar></div></div><div aria-hidden="true" mat-ripple="" class="mat-ripple mat-tab-header-pagination mat-tab-header-pagination-after mat-elevation-z4 mat-tab-header-pagination-disabled"><div class="mat-tab-header-pagination-chevron"></div></div></mat-tab-header><div class="mat-tab-body-wrapper"><mat-tab-body role="tabpanel" class="mat-tab-body ng-tns-c50-25 mat-tab-body-active ng-star-inserted" id="mat-tab-content-6-0" aria-labelledby="mat-tab-label-6-0"><div class="mat-tab-body-content ng-tns-c50-25 ng-trigger ng-trigger-translateTab" style="transform:none"><aio-code class="ng-star-inserted" style=""><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from app/heroes/hero.component.ts">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">HeroComponent</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  </span><span class="lit">@</span><a href="api/core/Output" class="code-anchor"><span class="lit">Output</span></a><span class="pun">()</span><span class="pln"> savedTheDay </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><a href="api/core/EventEmitter" class="code-anchor"><span class="typ">EventEmitter</span></a><span class="str">&lt;boolean&gt;</span><span class="pun">();</span><span class="pln">
</span><span class="pun">}</span></code>
    </pre></aio-code></div></mat-tab-body><mat-tab-body role="tabpanel" class="mat-tab-body ng-tns-c50-26 ng-star-inserted" id="mat-tab-content-6-1" aria-labelledby="mat-tab-label-6-1"><div class="mat-tab-body-content ng-tns-c50-26 ng-trigger ng-trigger-translateTab" style="transform:translate3d(100%,0,0);min-height:1px"></div></mat-tab-body></div></mat-tab-group></mat-card></code-tabs><p translation-result="on"><a href="guide/styleguide#toc">回到顶部</a></p><p translation-origin="off"><a href="guide/styleguide#toc">Back to top</a></p><a id="05-17"></a><h3 id="put-presentation-logic-in-the-component-class" translation-result="on">把表现层逻辑放到组件类里<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#put-presentation-logic-in-the-component-class"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="put-presentation-logic-in-the-component-class">Put presentation logic in the component class<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#put-presentation-logic-in-the-component-class"><i class="material-icons">link</i></a></h3><h4 id="style-05-17" translation-result="on">风格 05-17<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-05-17"><i class="material-icons">link</i></a></h4><h4 translation-origin="off" id="style-05-17">Style 05-17<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-05-17"><i class="material-icons">link</i></a></h4><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>把表现层逻辑放进组件类中，而不要放在模板里。</p><p translation-origin="off"><strong>Do</strong> put presentation logic in the component class, and not in the template.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong>逻辑应该只出现在一个地方（组件类里）而不应分散在两个地方。</p><p translation-origin="off"><strong>Why?</strong> Logic will be contained in one place (the component class) instead of being spread in two places.</p></div><div class="s-why-last"><p translation-result="on"><strong>为何？</strong>将组件的表现层逻辑放到组件类而非模板里，可以增强测试性、维护性和重复使用性。</p><p translation-origin="off"><strong>Why?</strong> Keeping the component's presentation logic in the class instead of the template improves testability, maintainability, and reusability.</p></div><code-example path="styleguide/src/05-17/app/heroes/hero-list/hero-list.component.avoid.ts" region="example" header="app/heroes/hero-list/hero-list.component.ts" ng-version="9.0.0-rc.11" class="avoidFile"><div style="display:none">/* avoid */ @<a href="api/core/Component" class="code-anchor">Component</a>({ selector: 'toh-hero-list', template: ` &lt;section&gt; Our list of heroes: &lt;hero-profile *<a href="api/common/NgForOf" class="code-anchor">ngFor</a>="let hero of heroes" [hero]="hero"&gt; &lt;/hero-profile&gt; Total powers: {{totalPowers}}&lt;br&gt; Average power: {{totalPowers / heroes.length}} &lt;/section&gt; ` }) export class HeroListComponent { heroes: Hero[]; totalPowers: number; }</div><header class="ng-star-inserted">app/heroes/hero-list/hero-list.component.ts</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from app/heroes/hero-list/hero-list.component.ts">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="com">/* avoid */</span><span class="pln">

</span><span class="lit">@</span><a href="api/core/Component" class="code-anchor"><span class="lit">Component</span></a><span class="pun">({</span><span class="pln">
  selector</span><span class="pun">:</span><span class="pln"> </span><span class="str">'toh-hero-list'</span><span class="pun">,</span><span class="pln">
  </span><span class="kwd">template</span><span class="pun">:</span><span class="pln"> </span><span class="str">`
    &lt;section&gt;
      Our list of heroes:
      &lt;hero-profile *</span><a href="api/common/NgForOf" class="code-anchor"><span class="str">ngFor</span></a><span class="str">="let hero of heroes" [hero]="hero"&gt;
      &lt;/hero-profile&gt;
      Total powers: {{totalPowers}}&lt;br&gt;
      Average power: {{totalPowers / heroes.length}}
    &lt;/section&gt;
  `</span><span class="pln">
</span><span class="pun">})</span><span class="pln">
</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">HeroListComponent</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  heroes</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Hero</span><span class="pun">[];</span><span class="pln">
  totalPowers</span><span class="pun">:</span><span class="pln"> number</span><span class="pun">;</span><span class="pln">
</span><span class="pun">}</span></code>
    </pre></aio-code></code-example><code-example path="styleguide/src/05-17/app/heroes/hero-list/hero-list.component.ts" region="example" header="app/heroes/hero-list/hero-list.component.ts" ng-version="9.0.0-rc.11"><div style="display:none">@<a href="api/core/Component" class="code-anchor">Component</a>({ selector: 'toh-hero-list', template: ` &lt;section&gt; Our list of heroes: &lt;toh-hero *<a href="api/common/NgForOf" class="code-anchor">ngFor</a>="let hero of heroes" [hero]="hero"&gt; &lt;/toh-hero&gt; Total powers: {{totalPowers}}&lt;br&gt; Average power: {{avgPower}} &lt;/section&gt; ` }) export class HeroListComponent { heroes: Hero[]; totalPowers: number; get avgPower() { return this.totalPowers / this.heroes.length; } }</div><header class="ng-star-inserted">app/heroes/hero-list/hero-list.component.ts</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from app/heroes/hero-list/hero-list.component.ts">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="lit">@</span><a href="api/core/Component" class="code-anchor"><span class="lit">Component</span></a><span class="pun">({</span><span class="pln">
  selector</span><span class="pun">:</span><span class="pln"> </span><span class="str">'toh-hero-list'</span><span class="pun">,</span><span class="pln">
  </span><span class="kwd">template</span><span class="pun">:</span><span class="pln"> </span><span class="str">`
    &lt;section&gt;
      Our list of heroes:
      &lt;toh-hero *</span><a href="api/common/NgForOf" class="code-anchor"><span class="str">ngFor</span></a><span class="str">="let hero of heroes" [hero]="hero"&gt;
      &lt;/toh-hero&gt;
      Total powers: {{totalPowers}}&lt;br&gt;
      Average power: {{avgPower}}
    &lt;/section&gt;
  `</span><span class="pln">
</span><span class="pun">})</span><span class="pln">
</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">HeroListComponent</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  heroes</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Hero</span><span class="pun">[];</span><span class="pln">
  totalPowers</span><span class="pun">:</span><span class="pln"> number</span><span class="pun">;</span><span class="pln">

  </span><span class="kwd">get</span><span class="pln"> avgPower</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="kwd">return</span><span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">totalPowers </span><span class="pun">/</span><span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">heroes</span><span class="pun">.</span><span class="pln">length</span><span class="pun">;</span><span class="pln">
  </span><span class="pun">}</span><span class="pln">
</span><span class="pun">}</span></code>
    </pre></aio-code></code-example><p translation-result="on"><a href="guide/styleguide#toc">回到顶部</a></p><p translation-origin="off"><a href="guide/styleguide#toc">Back to top</a></p><h2 id="directives" translation-result="on">指令<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#directives"><i class="material-icons">link</i></a></h2><h2 translation-origin="off" id="directives">Directives<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#directives"><i class="material-icons">link</i></a></h2><a id="06-01"></a><h3 id="use-directives-to-enhance-an-element" translation-result="on">使用指令来增强已有元素<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#use-directives-to-enhance-an-element"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="use-directives-to-enhance-an-element">Use directives to enhance an element<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#use-directives-to-enhance-an-element"><i class="material-icons">link</i></a></h3><h4 id="style-06-01" translation-result="on">风格 06-01<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-06-01"><i class="material-icons">link</i></a></h4><h4 translation-origin="off" id="style-06-01">Style 06-01<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-06-01"><i class="material-icons">link</i></a></h4><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>当你需要有表现层逻辑，但没有模板时，使用属性型指令。</p><p translation-origin="off"><strong>Do</strong> use attribute directives when you have presentation logic without a template.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong>属性型指令没有模板。</p><p translation-origin="off"><strong>Why?</strong> Attribute directives don't have an associated template.</p></div><div class="s-why-last"><p translation-result="on"><strong>为何？</strong>一个元素可以使用多个属性型指令。</p><p translation-origin="off"><strong>Why?</strong> An element may have more than one attribute directive applied.</p></div><code-example path="styleguide/src/06-01/app/shared/highlight.directive.ts" region="example" header="app/shared/highlight.directive.ts" ng-version="9.0.0-rc.11"><div style="display:none">@<a href="api/core/Directive" class="code-anchor">Directive</a>({ selector: '[tohHighlight]' }) export class HighlightDirective { @<a href="api/core/HostListener" class="code-anchor">HostListener</a>('mouseover') onMouseEnter() { // do highlight work } }</div><header class="ng-star-inserted">app/shared/highlight.directive.ts</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from app/shared/highlight.directive.ts">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="lit">@</span><a href="api/core/Directive" class="code-anchor"><span class="lit">Directive</span></a><span class="pun">({</span><span class="pln">
  selector</span><span class="pun">:</span><span class="pln"> </span><span class="str">'[tohHighlight]'</span><span class="pln">
</span><span class="pun">})</span><span class="pln">
</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">HighlightDirective</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  </span><span class="lit">@</span><a href="api/core/HostListener" class="code-anchor"><span class="lit">HostListener</span></a><span class="pun">(</span><span class="str">'mouseover'</span><span class="pun">)</span><span class="pln"> onMouseEnter</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="com">// do highlight work</span><span class="pln">
  </span><span class="pun">}</span><span class="pln">
</span><span class="pun">}</span></code>
    </pre></aio-code></code-example><code-example path="styleguide/src/06-01/app/app.component.html" header="app/app.component.html" ng-version="9.0.0-rc.11"><div style="display:none">&lt;div tohHighlight&gt;Bombasta&lt;/div&gt;</div><header class="ng-star-inserted">app/app.component.html</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from app/app.component.html">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="tag">&lt;div</span><span class="pln"> </span><span class="atn">tohHighlight</span><span class="tag">&gt;</span><span class="pln">Bombasta</span><span class="tag">&lt;/div&gt;</span></code>
    </pre></aio-code></code-example><p translation-result="on"><a href="guide/styleguide#toc">回到顶部</a></p><p translation-origin="off"><a href="guide/styleguide#toc">Back to top</a></p><a id="06-03"></a><h3 id="hostlistenerhostbinding-decorators-versus-host-metadata" translation-result="on"><em>HostListener</em> 和 <em>HostBinding</em> 装饰器 vs. 组件元数据 <em>host</em><a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#hostlistenerhostbinding-decorators-versus-host-metadata"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="hostlistenerhostbinding-decorators-versus-host-metadata"><em>HostListener</em>/<em>HostBinding</em> decorators versus <em>host</em> metadata<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#hostlistenerhostbinding-decorators-versus-host-metadata"><i class="material-icons">link</i></a></h3><h4 id="style-06-03" translation-result="on">风格 06-03<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-06-03"><i class="material-icons">link</i></a></h4><h4 translation-origin="off" id="style-06-03">Style 06-03<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-06-03"><i class="material-icons">link</i></a></h4><div class="s-rule consider"><p translation-result="on"><strong>考虑</strong>优先使用 <code>@<a href="api/core/HostListener" class="code-anchor">HostListener</a></code> 和 <code>@<a href="api/core/HostBinding" class="code-anchor">HostBinding</a></code>，而不是 <code>@<a href="api/core/Directive" class="code-anchor">Directive</a></code> 和 <code>@<a href="api/core/Component" class="code-anchor">Component</a></code> 装饰器的 <code><a href="api/core/Directive#host" class="code-anchor">host</a></code> 属性。</p><p translation-origin="off"><strong>Consider</strong> preferring the <code>@<a href="api/core/HostListener" class="code-anchor">HostListener</a></code> and <code>@<a href="api/core/HostBinding" class="code-anchor">HostBinding</a></code> to the <code><a href="api/core/Directive#host" class="code-anchor">host</a></code> property of the <code>@<a href="api/core/Directive" class="code-anchor">Directive</a></code> and <code>@<a href="api/core/Component" class="code-anchor">Component</a></code> decorators.</p></div><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>让你的选择保持一致。</p><p translation-origin="off"><strong>Do</strong> be consistent in your choice.</p></div><div class="s-why-last"><p translation-result="on"><strong>为何？</strong>对于关联到 <code>@<a href="api/core/HostBinding" class="code-anchor">HostBinding</a></code> 的属性或关联到 <code>@<a href="api/core/HostListener" class="code-anchor">HostListener</a></code> 的方法，要修改时，只需在指令类中的一个地方修改。 如果使用元数据属性 <code><a href="api/core/Directive#host" class="code-anchor">host</a></code>，你就得在组件类中修改属性声明的同时修改相关的元数据。</p><p translation-origin="off"><strong>Why?</strong> The property associated with <code>@<a href="api/core/HostBinding" class="code-anchor">HostBinding</a></code> or the method associated with <code>@<a href="api/core/HostListener" class="code-anchor">HostListener</a></code> can be modified only in a single place—in the directive's class. If you use the <code><a href="api/core/Directive#host" class="code-anchor">host</a></code> metadata property, you must modify both the property/method declaration in the directive's class and the metadata in the decorator associated with the directive.</p></div><code-example path="styleguide/src/06-03/app/shared/validator.directive.ts" header="app/shared/validator.directive.ts" ng-version="9.0.0-rc.11"><div style="display:none">import { <a href="api/core/Directive" class="code-anchor">Directive</a>, <a href="api/core/HostBinding" class="code-anchor">HostBinding</a>, <a href="api/core/HostListener" class="code-anchor">HostListener</a> } from '@angular/core'; @<a href="api/core/Directive" class="code-anchor">Directive</a>({ selector: '[tohValidator]' }) export class ValidatorDirective { @<a href="api/core/HostBinding" class="code-anchor">HostBinding</a>('attr.role') role = 'button'; @<a href="api/core/HostListener" class="code-anchor">HostListener</a>('mouseenter') onMouseEnter() { // do work } }</div><header class="ng-star-inserted">app/shared/validator.directive.ts</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from app/shared/validator.directive.ts">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="kwd">import</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><a href="api/core/Directive" class="code-anchor"><span class="typ">Directive</span></a><span class="pun">,</span><span class="pln"> </span><a href="api/core/HostBinding" class="code-anchor"><span class="typ">HostBinding</span></a><span class="pun">,</span><span class="pln"> </span><a href="api/core/HostListener" class="code-anchor"><span class="typ">HostListener</span></a><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">from</span><span class="pln"> </span><span class="str">'@angular/core'</span><span class="pun">;</span><span class="pln">

</span><span class="lit">@</span><a href="api/core/Directive" class="code-anchor"><span class="lit">Directive</span></a><span class="pun">({</span><span class="pln">
  selector</span><span class="pun">:</span><span class="pln"> </span><span class="str">'[tohValidator]'</span><span class="pln">
</span><span class="pun">})</span><span class="pln">
</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">ValidatorDirective</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  </span><span class="lit">@</span><a href="api/core/HostBinding" class="code-anchor"><span class="lit">HostBinding</span></a><span class="pun">(</span><span class="str">'attr.role'</span><span class="pun">)</span><span class="pln"> role </span><span class="pun">=</span><span class="pln"> </span><span class="str">'button'</span><span class="pun">;</span><span class="pln">
  </span><span class="lit">@</span><a href="api/core/HostListener" class="code-anchor"><span class="lit">HostListener</span></a><span class="pun">(</span><span class="str">'mouseenter'</span><span class="pun">)</span><span class="pln"> onMouseEnter</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="com">// do work</span><span class="pln">
  </span><span class="pun">}</span><span class="pln">
</span><span class="pun">}</span></code>
    </pre></aio-code></code-example><p translation-result="on">与不推荐的方式（<code><a href="api/core/Directive#host" class="code-anchor">host</a></code> 元数据）比较一下。</p><p translation-origin="off">Compare with the less preferred <code><a href="api/core/Directive#host" class="code-anchor">host</a></code> metadata alternative.</p><div class="s-why-last"><p translation-result="on"><strong>为何？</strong><code><a href="api/core/Directive#host" class="code-anchor">host</a></code> 元数据只是一个便于记忆的名字而已，并不需要额外的 ES 导入。</p><p translation-origin="off"><strong>Why?</strong> The <code><a href="api/core/Directive#host" class="code-anchor">host</a></code> metadata is only one term to remember and doesn't require extra ES imports.</p></div><code-example path="styleguide/src/06-03/app/shared/validator2.directive.ts" header="app/shared/validator2.directive.ts" ng-version="9.0.0-rc.11"><div style="display:none">import { <a href="api/core/Directive" class="code-anchor">Directive</a> } from '@angular/core'; @<a href="api/core/Directive" class="code-anchor">Directive</a>({ selector: '[tohValidator2]', <a href="api/core/Directive#host" class="code-anchor">host</a>: { '[attr.role]': 'role', '(mouseenter)': 'onMouseEnter()' } }) export class Validator2Directive { role = 'button'; onMouseEnter() { // do work } }</div><header class="ng-star-inserted">app/shared/validator2.directive.ts</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from app/shared/validator2.directive.ts">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="kwd">import</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><a href="api/core/Directive" class="code-anchor"><span class="typ">Directive</span></a><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">from</span><span class="pln"> </span><span class="str">'@angular/core'</span><span class="pun">;</span><span class="pln">

</span><span class="lit">@</span><a href="api/core/Directive" class="code-anchor"><span class="lit">Directive</span></a><span class="pun">({</span><span class="pln">
  selector</span><span class="pun">:</span><span class="pln"> </span><span class="str">'[tohValidator2]'</span><span class="pun">,</span><span class="pln">
  </span><a href="api/core/Directive#host" class="code-anchor"><span class="pln">host</span></a><span class="pun">:</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="str">'[attr.role]'</span><span class="pun">:</span><span class="pln"> </span><span class="str">'role'</span><span class="pun">,</span><span class="pln">
    </span><span class="str">'(mouseenter)'</span><span class="pun">:</span><span class="pln"> </span><span class="str">'onMouseEnter()'</span><span class="pln">
  </span><span class="pun">}</span><span class="pln">
</span><span class="pun">})</span><span class="pln">
</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">Validator2Directive</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  role </span><span class="pun">=</span><span class="pln"> </span><span class="str">'button'</span><span class="pun">;</span><span class="pln">
  onMouseEnter</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="com">// do work</span><span class="pln">
  </span><span class="pun">}</span><span class="pln">
</span><span class="pun">}</span></code>
    </pre></aio-code></code-example><p translation-result="on"><a href="guide/styleguide#toc">回到顶部</a></p><p translation-origin="off"><a href="guide/styleguide#toc">Back to top</a></p><h2 id="services" translation-result="on">服务<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#services"><i class="material-icons">link</i></a></h2><h2 translation-origin="off" id="services">Services<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#services"><i class="material-icons">link</i></a></h2><a id="07-01"></a><h3 id="services-are-singletons" translation-result="on">服务总是单例的<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#services-are-singletons"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="services-are-singletons">Services are singletons<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#services-are-singletons"><i class="material-icons">link</i></a></h3><h4 id="style-07-01" translation-result="on">风格 07-01<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-07-01"><i class="material-icons">link</i></a></h4><h4 translation-origin="off" id="style-07-01">Style 07-01<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-07-01"><i class="material-icons">link</i></a></h4><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>在同一个注入器内，把服务当做单例使用。用它们来共享数据和功能。</p><p translation-origin="off"><strong>Do</strong> use services as singletons within the same injector. Use them for sharing data and functionality.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong>服务是在特性范围或应用内共享方法的理想载体。</p><p translation-origin="off"><strong>Why?</strong> Services are ideal for sharing methods across a feature area or an app.</p></div><div class="s-why-last"><p translation-result="on"><strong>为何？</strong>服务是共享状态性内存数据的理想载体。</p><p translation-origin="off"><strong>Why?</strong> Services are ideal for sharing stateful in-memory data.</p></div><code-example path="styleguide/src/07-01/app/heroes/shared/hero.service.ts" region="example" header="app/heroes/shared/hero.service.ts" ng-version="9.0.0-rc.11"><div style="display:none">export class HeroService { constructor(private <a href="api/common/http" class="code-anchor">http</a>: <a href="api/common/http/HttpClient" class="code-anchor">HttpClient</a>) { } getHeroes() { return this.http.get&lt;Hero[]&gt;('api/heroes'); } }</div><header class="ng-star-inserted">app/heroes/shared/hero.service.ts</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from app/heroes/shared/hero.service.ts">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">HeroService</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  </span><span class="kwd">constructor</span><span class="pun">(</span><span class="kwd">private</span><span class="pln"> </span><a href="api/common/http" class="code-anchor"><span class="pln">http</span></a><span class="pun">:</span><span class="pln"> </span><a href="api/common/http/HttpClient" class="code-anchor"><span class="typ">HttpClient</span></a><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="pun">}</span><span class="pln">

  getHeroes</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="kwd">return</span><span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">http</span><span class="pun">.</span><span class="kwd">get</span><span class="pun">&lt;</span><span class="typ">Hero</span><span class="pun">[]&gt;(</span><span class="str">'api/heroes'</span><span class="pun">);</span><span class="pln">
  </span><span class="pun">}</span><span class="pln">
</span><span class="pun">}</span></code>
    </pre></aio-code></code-example><p translation-result="on"><a href="guide/styleguide#toc">回到顶部</a></p><p translation-origin="off"><a href="guide/styleguide#toc">Back to top</a></p><a id="07-02"></a><h3 id="single-responsibility-1" translation-result="on">单一职责<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#single-responsibility-1"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="single-responsibility-1">Single responsibility<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#single-responsibility-1"><i class="material-icons">link</i></a></h3><h4 id="style-07-02" translation-result="on">风格 07-02<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-07-02"><i class="material-icons">link</i></a></h4><h4 translation-origin="off" id="style-07-02">Style 07-02<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-07-02"><i class="material-icons">link</i></a></h4><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>创建封装在上下文中的单一职责的服务。</p><p translation-origin="off"><strong>Do</strong> create services with a single responsibility that is encapsulated by its context.</p></div><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>当服务成长到超出单一用途时，创建一个新服务。</p><p translation-origin="off"><strong>Do</strong> create a new service once the service begins to exceed that singular purpose.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong>当服务有多个职责时，它很难被测试。</p><p translation-origin="off"><strong>Why?</strong> When a service has multiple responsibilities, it becomes difficult to test.</p></div><div class="s-why-last"><p translation-result="on"><strong>为何？</strong>当某个服务有多个职责时，每个注入它的组件或服务都会承担这些职责的全部开销。</p><p translation-origin="off"><strong>Why?</strong> When a service has multiple responsibilities, every component or service that injects it now carries the weight of them all.</p></div><p translation-result="on"><a href="guide/styleguide#toc">回到顶部</a></p><p translation-origin="off"><a href="guide/styleguide#toc">Back to top</a></p><a id="07-03"></a><h3 id="providing-a-service" translation-result="on">提供一个服务<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#providing-a-service"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="providing-a-service">Providing a service<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#providing-a-service"><i class="material-icons">link</i></a></h3><h4 id="style-07-03" translation-result="on">风格 07-03<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-07-03"><i class="material-icons">link</i></a></h4><h4 translation-origin="off" id="style-07-03">Style 07-03<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-07-03"><i class="material-icons">link</i></a></h4><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>在服务的 <code>@<a href="api/core/Injectable" class="code-anchor">Injectable</a></code> 装饰器上指定通过应用的根注入器提供服务。</p><p translation-origin="off"><strong>Do</strong> provide a service with the app root injector in the <code>@<a href="api/core/Injectable" class="code-anchor">Injectable</a></code> decorator of the service.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong> Angular 注入器是层次化的。</p><p translation-origin="off"><strong>Why?</strong> The Angular injector is hierarchical.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong>当你在根注入器上提供该服务时，该服务实例在每个需要该服务的类中是共享的。当服务要共享方法或状态时，这是最理想的选择。</p><p translation-origin="off"><strong>Why?</strong> When you provide the service to a root injector, that instance of the service is shared and available in every class that needs the service. This is ideal when a service is sharing methods or state.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong>当你在服务的 <code>@<a href="api/core/Injectable" class="code-anchor">Injectable</a></code> 中注册服务时，<a href="cli">Angular CLI</a> 生产环境构建时使用的优化工具可以进行摇树优化，从而移除那些你的应用中从未用过的服务。</p><p translation-origin="off"><strong>Why?</strong> When you register a service in the <code>@<a href="api/core/Injectable" class="code-anchor">Injectable</a></code> decorator of the service, optimization tools such as those used by the <a href="cli">Angular CLI's</a> production builds can perform tree shaking and remove services that aren't used by your app.</p></div><div class="s-why-last"><p translation-result="on"><strong>为何？</strong>当不同的两个组件需要一个服务的不同的实例时，上面的方法这就不理想了。在这种情况下，对于需要崭新和单独服务实例的组件，最好在组件级提供服务。</p><p translation-origin="off"><strong>Why?</strong> This is not ideal when two different components need different instances of a service. In this scenario it would be better to provide the service at the component level that needs the new and separate instance.</p></div><code-example path="dependency-injection/src/app/tree-shaking/service.ts" header="src/app/treeshaking/service.ts" ng-version="9.0.0-rc.11"><div style="display:none">@<a href="api/core/Injectable" class="code-anchor">Injectable</a>({ <a href="api/core/Injectable#providedIn" class="code-anchor">providedIn</a>: 'root', }) export class Service { }</div><header class="ng-star-inserted">src/app/treeshaking/service.ts</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from src/app/treeshaking/service.ts">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="lit">@</span><a href="api/core/Injectable" class="code-anchor"><span class="lit">Injectable</span></a><span class="pun">({</span><span class="pln">
  </span><a href="api/core/Injectable#providedIn" class="code-anchor"><span class="pln">providedIn</span></a><span class="pun">:</span><span class="pln"> </span><span class="str">'root'</span><span class="pun">,</span><span class="pln">
</span><span class="pun">})</span><span class="pln">
</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">Service</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
</span><span class="pun">}</span></code>
    </pre></aio-code></code-example><p translation-result="on"><a href="guide/styleguide#toc">回到顶部</a></p><p translation-origin="off"><a href="guide/styleguide#toc">Back to top</a></p><a id="07-04"></a><h3 id="use-the-injectable-class-decorator" translation-result="on">使用 @Injectable() 类装饰器<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#use-the-injectable-class-decorator"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="use-the-injectable-class-decorator">Use the @Injectable() class decorator<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#use-the-injectable-class-decorator"><i class="material-icons">link</i></a></h3><h4 id="style-07-04" translation-result="on">风格 07-04<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-07-04"><i class="material-icons">link</i></a></h4><h4 translation-origin="off" id="style-07-04">Style 07-04<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-07-04"><i class="material-icons">link</i></a></h4><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>当使用类型作为令牌来注入服务的依赖时，使用 <code>@<a href="api/core/Injectable" class="code-anchor">Injectable</a>()</code> 类装饰器，而非 <code>@<a href="api/core/Inject" class="code-anchor">Inject</a>()</code> 参数装饰器。</p><p translation-origin="off"><strong>Do</strong> use the <code>@<a href="api/core/Injectable" class="code-anchor">Injectable</a>()</code> class decorator instead of the <code>@<a href="api/core/Inject" class="code-anchor">Inject</a></code> parameter decorator when using types as tokens for the dependencies of a service.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong> Angular 的 DI 机制会根据服务的构造函数参数的声明类型来解析服务的所有依赖。</p><p translation-origin="off"><strong>Why?</strong> The Angular Dependency Injection (DI) mechanism resolves a service's own dependencies based on the declared types of that service's constructor parameters.</p></div><div class="s-why-last"><p translation-result="on"><strong>为何？</strong>当服务只接受类型令牌相关的依赖时，比起在每个构造函数参数上使用 <code>@<a href="api/core/Inject" class="code-anchor">Inject</a>()</code>，<code>@<a href="api/core/Injectable" class="code-anchor">Injectable</a>()</code> 的语法简洁多了。</p><p translation-origin="off"><strong>Why?</strong> When a service accepts only dependencies associated with type tokens, the <code>@<a href="api/core/Injectable" class="code-anchor">Injectable</a>()</code> syntax is much less verbose compared to using <code>@<a href="api/core/Inject" class="code-anchor">Inject</a>()</code> on each individual constructor parameter.</p></div><code-example path="styleguide/src/07-04/app/heroes/shared/hero-arena.service.avoid.ts" region="example" header="app/heroes/shared/hero-arena.service.ts" ng-version="9.0.0-rc.11" class="avoidFile"><div style="display:none">/* avoid */ export class HeroArena { constructor( @<a href="api/core/Inject" class="code-anchor">Inject</a>(HeroService) private heroService: HeroService, @<a href="api/core/Inject" class="code-anchor">Inject</a>(<a href="api/common/http/HttpClient" class="code-anchor">HttpClient</a>) private <a href="api/common/http" class="code-anchor">http</a>: <a href="api/common/http/HttpClient" class="code-anchor">HttpClient</a>) {} }</div><header class="ng-star-inserted">app/heroes/shared/hero-arena.service.ts</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from app/heroes/shared/hero-arena.service.ts">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="com">/* avoid */</span><span class="pln">

</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">HeroArena</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  </span><span class="kwd">constructor</span><span class="pun">(</span><span class="pln">
      </span><span class="lit">@</span><a href="api/core/Inject" class="code-anchor"><span class="lit">Inject</span></a><span class="pun">(</span><span class="typ">HeroService</span><span class="pun">)</span><span class="pln"> </span><span class="kwd">private</span><span class="pln"> heroService</span><span class="pun">:</span><span class="pln"> </span><span class="typ">HeroService</span><span class="pun">,</span><span class="pln">
      </span><span class="lit">@</span><a href="api/core/Inject" class="code-anchor"><span class="lit">Inject</span></a><span class="pun">(</span><a href="api/common/http/HttpClient" class="code-anchor"><span class="typ">HttpClient</span></a><span class="pun">)</span><span class="pln"> </span><span class="kwd">private</span><span class="pln"> </span><a href="api/common/http" class="code-anchor"><span class="pln">http</span></a><span class="pun">:</span><span class="pln"> </span><a href="api/common/http/HttpClient" class="code-anchor"><span class="typ">HttpClient</span></a><span class="pun">)</span><span class="pln"> </span><span class="pun">{}</span><span class="pln">
</span><span class="pun">}</span></code>
    </pre></aio-code></code-example><code-example path="styleguide/src/07-04/app/heroes/shared/hero-arena.service.ts" region="example" header="app/heroes/shared/hero-arena.service.ts" ng-version="9.0.0-rc.11"><div style="display:none">@<a href="api/core/Injectable" class="code-anchor">Injectable</a>() export class HeroArena { constructor( private heroService: HeroService, private <a href="api/common/http" class="code-anchor">http</a>: <a href="api/common/http/HttpClient" class="code-anchor">HttpClient</a>) {} }</div><header class="ng-star-inserted">app/heroes/shared/hero-arena.service.ts</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from app/heroes/shared/hero-arena.service.ts">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="lit">@</span><a href="api/core/Injectable" class="code-anchor"><span class="lit">Injectable</span></a><span class="pun">()</span><span class="pln">
</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">HeroArena</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  </span><span class="kwd">constructor</span><span class="pun">(</span><span class="pln">
    </span><span class="kwd">private</span><span class="pln"> heroService</span><span class="pun">:</span><span class="pln"> </span><span class="typ">HeroService</span><span class="pun">,</span><span class="pln">
    </span><span class="kwd">private</span><span class="pln"> </span><a href="api/common/http" class="code-anchor"><span class="pln">http</span></a><span class="pun">:</span><span class="pln"> </span><a href="api/common/http/HttpClient" class="code-anchor"><span class="typ">HttpClient</span></a><span class="pun">)</span><span class="pln"> </span><span class="pun">{}</span><span class="pln">
</span><span class="pun">}</span></code>
    </pre></aio-code></code-example><p translation-result="on"><a href="guide/styleguide#toc">回到顶部</a></p><p translation-origin="off"><a href="guide/styleguide#toc">Back to top</a></p><h2 id="data-services" translation-result="on">数据服务<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#data-services"><i class="material-icons">link</i></a></h2><h2 translation-origin="off" id="data-services">Data Services<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#data-services"><i class="material-icons">link</i></a></h2><a id="08-01"></a><h3 id="talk-to-the-server-through-a-service" translation-result="on">通过服务与 Web 服务器通讯<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#talk-to-the-server-through-a-service"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="talk-to-the-server-through-a-service">Talk to the server through a service<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#talk-to-the-server-through-a-service"><i class="material-icons">link</i></a></h3><h4 id="style-08-01" translation-result="on">风格 08-01<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-08-01"><i class="material-icons">link</i></a></h4><h4 translation-origin="off" id="style-08-01">Style 08-01<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-08-01"><i class="material-icons">link</i></a></h4><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>把数据操作和与数据交互的逻辑重构到服务里。</p><p translation-origin="off"><strong>Do</strong> refactor logic for making data operations and interacting with data to a service.</p></div><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>让数据服务来负责 XHR 调用、本地储存、内存储存或者其它数据操作。</p><p translation-origin="off"><strong>Do</strong> make data services responsible for XHR calls, local storage, stashing in memory, or any other data operations.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong>组件的职责是为视图展示或收集信息。它不应该关心如何获取数据，它只需要知道向谁请求数据。把如何获取数据的逻辑移动到数据服务里，简化了组件，让其聚焦于视图。</p><p translation-origin="off"><strong>Why?</strong> The component's responsibility is for the presentation and gathering of information for the view. It should not care how it gets the data, just that it knows who to ask for it. Separating the data services moves the logic on how to get it to the data service, and lets the component be simpler and more focused on the view.</p></div><div class="s-why"><p translation-result="on"><strong>为何？</strong>在测试使用数据服务的组件时，可以让数据调用更容易被测试（模拟或者真实）。</p><p translation-origin="off"><strong>Why?</strong> This makes it easier to test (mock or real) the data calls when testing a component that uses a data service.</p></div><div class="s-why-last"><p translation-result="on"><strong>为何？</strong>数据管理的详情，比如头信息、方法、缓存、错误处理和重试逻辑，不是组件和其它的数据消费者应该关心的事情。</p><p translation-origin="off"><strong>Why?</strong> The details of data management, such as headers, HTTP methods, caching, error handling, and retry logic, are irrelevant to components and other data consumers.</p><p translation-result="on">数据服务应该封装这些细节。这样，在服务内部修改细节，就不会影响到它的消费者。并且更容易通过实现一个模拟服务来对消费者进行测试。</p><p translation-origin="off">A data service encapsulates these details. It's easier to evolve these details inside the service without affecting its consumers. And it's easier to test the consumers with mock service implementations.</p></div><p translation-result="on"><a href="guide/styleguide#toc">回到顶部</a></p><p translation-origin="off"><a href="guide/styleguide#toc">Back to top</a></p><h2 id="lifecycle-hooks" translation-result="on">生命周期钩子<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#lifecycle-hooks"><i class="material-icons">link</i></a></h2><h2 translation-origin="off" id="lifecycle-hooks">Lifecycle hooks<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#lifecycle-hooks"><i class="material-icons">link</i></a></h2><p translation-result="on">使用生命周期钩子来介入到 Angular 暴露的重要事件里。</p><p translation-origin="off">Use Lifecycle hooks to tap into important events exposed by Angular.</p><p translation-result="on"><a href="guide/styleguide#toc">回到顶部</a></p><p translation-origin="off"><a href="guide/styleguide#toc">Back to top</a></p><a id="09-01"></a><h3 id="implement-lifecycle-hook-interfaces" translation-result="on">实现生命周期钩子接口<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#implement-lifecycle-hook-interfaces"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="implement-lifecycle-hook-interfaces">Implement lifecycle hook interfaces<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#implement-lifecycle-hook-interfaces"><i class="material-icons">link</i></a></h3><h4 id="style-09-01" translation-result="on">风格 09-01<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-09-01"><i class="material-icons">link</i></a></h4><h4 translation-origin="off" id="style-09-01">Style 09-01<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-09-01"><i class="material-icons">link</i></a></h4><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>实现生命周期钩子接口。</p><p translation-origin="off"><strong>Do</strong> implement the lifecycle hook interfaces.</p></div><div class="s-why-last"><p translation-result="on"><strong>为何？</strong>如果使用强类型的方法签名，编译器和编辑器可以帮你揪出拼写错误。</p><p translation-origin="off"><strong>Why?</strong> Lifecycle interfaces prescribe typed method signatures. Use those signatures to flag spelling and syntax mistakes.</p></div><code-example path="styleguide/src/09-01/app/heroes/shared/hero-button/hero-button.component.avoid.ts" region="example" header="app/heroes/shared/hero-button/hero-button.component.ts" ng-version="9.0.0-rc.11" class="avoidFile"><div style="display:none">/* avoid */ @<a href="api/core/Component" class="code-anchor">Component</a>({ selector: 'toh-hero-button', template: `&lt;button&gt;OK&lt;button&gt;` }) export class HeroButtonComponent { <a href="" class="code-anchor">onInit</a>() { // misspelled console.log('The component is initialized'); } }</div><header class="ng-star-inserted">app/heroes/shared/hero-button/hero-button.component.ts</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from app/heroes/shared/hero-button/hero-button.component.ts">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="com">/* avoid */</span><span class="pln">

</span><span class="lit">@</span><a href="api/core/Component" class="code-anchor"><span class="lit">Component</span></a><span class="pun">({</span><span class="pln">
  selector</span><span class="pun">:</span><span class="pln"> </span><span class="str">'toh-hero-button'</span><span class="pun">,</span><span class="pln">
  </span><span class="kwd">template</span><span class="pun">:</span><span class="pln"> </span><span class="str">`&lt;button&gt;OK&lt;button&gt;`</span><span class="pln">
</span><span class="pun">})</span><span class="pln">
</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">HeroButtonComponent</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  </span><a href="" class="code-anchor"><span class="pln">onInit</span></a><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="com">// misspelled</span><span class="pln">
    console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">'The component is initialized'</span><span class="pun">);</span><span class="pln">
  </span><span class="pun">}</span><span class="pln">
</span><span class="pun">}</span></code>
    </pre></aio-code></code-example><code-example path="styleguide/src/09-01/app/heroes/shared/hero-button/hero-button.component.ts" region="example" header="app/heroes/shared/hero-button/hero-button.component.ts" ng-version="9.0.0-rc.11"><div style="display:none">@<a href="api/core/Component" class="code-anchor">Component</a>({ selector: 'toh-hero-button', template: `&lt;button&gt;OK&lt;/button&gt;` }) export class HeroButtonComponent implements <a href="api/core/OnInit" class="code-anchor">OnInit</a> { ngOnInit() { console.log('The component is initialized'); } }</div><header class="ng-star-inserted">app/heroes/shared/hero-button/hero-button.component.ts</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from app/heroes/shared/hero-button/hero-button.component.ts">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="lit">@</span><a href="api/core/Component" class="code-anchor"><span class="lit">Component</span></a><span class="pun">({</span><span class="pln">
  selector</span><span class="pun">:</span><span class="pln"> </span><span class="str">'toh-hero-button'</span><span class="pun">,</span><span class="pln">
  </span><span class="kwd">template</span><span class="pun">:</span><span class="pln"> </span><span class="str">`&lt;button&gt;OK&lt;/button&gt;`</span><span class="pln">
</span><span class="pun">})</span><span class="pln">
</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">HeroButtonComponent</span><span class="pln"> </span><span class="kwd">implements</span><span class="pln"> </span><a href="api/core/OnInit" class="code-anchor"><span class="typ">OnInit</span></a><span class="pln"> </span><span class="pun">{</span><span class="pln">
  ngOnInit</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">'The component is initialized'</span><span class="pun">);</span><span class="pln">
  </span><span class="pun">}</span><span class="pln">
</span><span class="pun">}</span></code>
    </pre></aio-code></code-example><p translation-result="on"><a href="guide/styleguide#toc">回到顶部</a></p><p translation-origin="off"><a href="guide/styleguide#toc">Back to top</a></p><h2 id="appendix" translation-result="on">附录<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#appendix"><i class="material-icons">link</i></a></h2><h2 translation-origin="off" id="appendix">Appendix<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#appendix"><i class="material-icons">link</i></a></h2><p translation-result="on">有用的 Angular 工具和小提示</p><p translation-origin="off">Useful tools and tips for Angular.</p><p translation-result="on"><a href="guide/styleguide#toc">回到顶部</a></p><p translation-origin="off"><a href="guide/styleguide#toc">Back to top</a></p><a id="A-01"></a><h3 id="codelyzer">Codelyzer<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#codelyzer"><i class="material-icons">link</i></a></h3><h4 id="style-a-01" translation-result="on">风格 A-01<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-a-01"><i class="material-icons">link</i></a></h4><h4 translation-origin="off" id="style-a-01">Style A-01<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-a-01"><i class="material-icons">link</i></a></h4><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>使用 <a href="https://www.npmjs.com/package/codelyzer">codelyzer</a> 来实施本指南。</p><p translation-origin="off"><strong>Do</strong> use <a href="https://www.npmjs.com/package/codelyzer">codelyzer</a> to follow this guide.</p></div><div class="s-rule consider"><p translation-result="on"><strong>考虑</strong>调整 codelyzer 的规则来满足你的需求。</p><p translation-origin="off"><strong>Consider</strong> adjusting the rules in codelyzer to suit your needs.</p></div><p translation-result="on"><a href="guide/styleguide#toc">回到顶部</a></p><p translation-origin="off"><a href="guide/styleguide#toc">Back to top</a></p><a id="A-02"></a><h3 id="file-templates-and-snippets" translation-result="on">文档模板和代码片段<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#file-templates-and-snippets"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="file-templates-and-snippets">File templates and snippets<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#file-templates-and-snippets"><i class="material-icons">link</i></a></h3><h4 id="style-a-02" translation-result="on">风格 A-02<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-a-02"><i class="material-icons">link</i></a></h4><h4 translation-origin="off" id="style-a-02">Style A-02<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/styleguide#style-a-02"><i class="material-icons">link</i></a></h4><div class="s-rule do"><p translation-result="on"><strong>坚持</strong>使用文件模板或代码片段来帮助实现一致的风格和模式。下面是为一些网络开发编辑器和 IDE 准备的模板和/或代码片段：</p><p translation-origin="off"><strong>Do</strong> use file templates or snippets to help follow consistent styles and patterns. Here are templates and/or snippets for some of the web development editors and IDEs.</p></div><div class="s-rule consider"><p translation-result="on"><strong>考虑</strong>使用 <a href="https://code.visualstudio.com/">Visual Studio Code</a>的<a href="https://marketplace.visualstudio.com/items?itemName=johnpapa.Angular2">代码片段</a> 来实施本风格指南。</p><p translation-origin="off"><strong>Consider</strong> using <a href="https://marketplace.visualstudio.com/items?itemName=johnpapa.Angular2">snippets</a> for <a href="https://code.visualstudio.com/">Visual Studio Code</a> that follow these styles and guidelines.</p><a href="https://marketplace.visualstudio.com/items?itemName=johnpapa.Angular2"><img src="generated/images/guide/styleguide/use-extension.gif" alt="Use Extension" width="700" height="429"></a><p translation-result="on"><strong>考虑</strong>使用 <a href="https://atom.io/">Atom</a> 的<a href="https://atom.io/packages/angular-2-typescript-snippets">代码片断</a>来实施本风格指南。</p><p translation-origin="off"><strong>Consider</strong> using <a href="https://atom.io/packages/angular-2-typescript-snippets">snippets</a> for <a href="https://atom.io/">Atom</a> that follow these styles and guidelines.</p><p translation-result="on"><strong>考虑</strong>使用 <a href="http://www.sublimetext.com/">Sublime Text</a>的<a href="https://github.com/orizens/sublime-angular2-snippets">代码片断</a> 来实施本风格指南。</p><p translation-origin="off"><strong>Consider</strong> using <a href="https://github.com/orizens/sublime-angular2-snippets">snippets</a> for <a href="http://www.sublimetext.com/">Sublime Text</a> that follow these styles and guidelines.</p><p translation-result="on"><strong>考虑</strong>使用 <a href="http://www.vim.org/">Vim</a> 的<a href="https://github.com/mhartington/vim-angular2-snippets">代码片断</a>来实施本风格指南。</p><p translation-origin="off"><strong>Consider</strong> using <a href="https://github.com/mhartington/vim-angular2-snippets">snippets</a> for <a href="http://www.vim.org/">Vim</a> that follow these styles and guidelines.</p></div><p translation-result="on"><a href="guide/styleguide#toc">回到顶部</a></p><p translation-origin="off"><a href="guide/styleguide#toc">Back to top</a></p></div></div></aio-doc-viewer></main></mat-sidenav-content></mat-sidenav-container><div class="toc-container no-print ng-star-inserted"><aio-lazy-ce selector="aio-toc"><aio-toc ng-version="9.0.0-rc.11"><div class="toc-inner no-print collapsed ng-star-inserted"><ul class="toc-list"><li class="h1 ng-star-inserted active" title="风格指南"><a href="guide/styleguide#angular-coding-style-guide">风格指南</a></li><li class="h2 ng-star-inserted" title="风格指南的用词"><a href="guide/styleguide#style-vocabulary">风格指南的用词</a></li><li class="h2 ng-star-inserted" title="文件结构约定"><a href="guide/styleguide#file-structure-conventions">文件结构约定</a></li><li class="h2 ng-star-inserted" title="单一职责"><a href="guide/styleguide#single-responsibility">单一职责</a></li><li class="h3 ng-star-inserted" title="单一规则"><a href="guide/styleguide#rule-of-one">单一规则</a></li><li class="h3 ng-star-inserted" title="小函数"><a href="guide/styleguide#small-functions">小函数</a></li><li class="h2 ng-star-inserted" title="命名"><a href="guide/styleguide#naming">命名</a></li><li class="h3 ng-star-inserted" title="总体命名原则"><a href="guide/styleguide#general-naming-guidelines">总体命名原则</a></li><li class="h3 ng-star-inserted" title="使用点和横杠来分隔文件名"><a href="guide/styleguide#separate-file-names-with-dots-and-dashes">使用点和横杠来分隔文件名</a></li><li class="h3 ng-star-inserted" title="符号名与文件名"><a href="guide/styleguide#symbols-and-file-names">符号名与文件名</a></li><li class="h3 ng-star-inserted" title="服务名"><a href="guide/styleguide#service-names">服务名</a></li><li class="h3 ng-star-inserted" title="引导"><a href="guide/styleguide#bootstrapping">引导</a></li><li class="h3 ng-star-inserted" title="组件选择器"><a href="guide/styleguide#component-selectors">组件选择器</a></li><li class="h3 ng-star-inserted" title="为组件添加自定义前缀"><a href="guide/styleguide#component-custom-prefix">为组件添加自定义前缀</a></li><li class="h3 ng-star-inserted" title="指令选择器"><a href="guide/styleguide#directive-selectors">指令选择器</a></li><li class="h3 ng-star-inserted" title="为指令添加自定义前缀"><a href="guide/styleguide#directive-custom-prefix">为指令添加自定义前缀</a></li><li class="h3 ng-star-inserted" title="管道名"><a href="guide/styleguide#pipe-names">管道名</a></li><li class="h3 ng-star-inserted" title="单元测试文件名"><a href="guide/styleguide#unit-test-file-names">单元测试文件名</a></li><li class="h3 ng-star-inserted" title="端到端（E2E）测试的文件名"><a href="guide/styleguide#end-to-end-e2e-test-file-names"><em>端到端</em>（E2E）测试的文件名</a></li><li class="h3 ng-star-inserted" title="Angular NgModule 命名"><a href="guide/styleguide#angular-ngmodule-names">Angular <em>NgModule</em> 命名</a></li><li class="h2 ng-star-inserted" title="应用程序结构与 NgModule"><a href="guide/styleguide#application-structure-and-ngmodules">应用程序结构与 NgModule</a></li><li class="h3 ng-star-inserted" title="LIFT"><a href="guide/styleguide#lift"><em>LIFT</em></a></li><li class="h3 ng-star-inserted" title="定位"><a href="guide/styleguide#locate">定位</a></li><li class="h3 ng-star-inserted" title="识别"><a href="guide/styleguide#identify">识别</a></li><li class="h3 ng-star-inserted" title="扁平"><a href="guide/styleguide#flat">扁平</a></li><li class="h3 ng-star-inserted" title="T-DRY（尽量不重复自己）"><a href="guide/styleguide#t-dry-try-to-be-dry"><em>T-DRY</em>（尽量不重复自己）</a></li><li class="h3 ng-star-inserted" title="总体结构的指导原则"><a href="guide/styleguide#overall-structural-guidelines">总体结构的指导原则</a></li><li class="h3 ng-star-inserted" title="按特性组织的目录结构"><a href="guide/styleguide#folders-by-feature-structure">按特性组织的目录结构</a></li><li class="h3 ng-star-inserted" title="应用的根模块"><a href="guide/styleguide#app-root-module">应用的<em>根模块</em></a></li><li class="h3 ng-star-inserted" title="特性模块"><a href="guide/styleguide#feature-modules">特性模块</a></li><li class="h3 ng-star-inserted" title="共享特性模块"><a href="guide/styleguide#shared-feature-module">共享特性模块</a></li><li class="h3 ng-star-inserted" title="惰性加载文件夹"><a href="guide/styleguide#lazy-loaded-folders">惰性加载文件夹</a></li><li class="h3 ng-star-inserted" title="永远不要直接导入惰性加载的目录"><a href="guide/styleguide#never-directly-import-lazy-loaded-folders">永远不要直接导入惰性加载的目录</a></li><li class="h2 ng-star-inserted" title="组件"><a href="guide/styleguide#components">组件</a></li><li class="h3 ng-star-inserted" title="把组件当做元素"><a href="guide/styleguide#components-as-elements">把组件当做元素</a></li><li class="h3 ng-star-inserted" title="把模板和样式提取到它们自己的文件"><a href="guide/styleguide#extract-templates-and-styles-to-their-own-files">把模板和样式提取到它们自己的文件</a></li><li class="h3 ng-star-inserted" title="内联输入和输出属性装饰器"><a href="guide/styleguide#decorate-input-and-output-properties">内联输入和输出属性装饰器</a></li><li class="h3 ng-star-inserted" title="避免为输入和输出属性指定别名"><a href="guide/styleguide#avoid-aliasing-inputs-and-outputs">避免为输入和输出属性指定别名</a></li><li class="h3 ng-star-inserted" title="成员顺序"><a href="guide/styleguide#member-sequence">成员顺序</a></li><li class="h3 ng-star-inserted" title="把逻辑放到服务里"><a href="guide/styleguide#delegate-complex-component-logic-to-services">把逻辑放到服务里</a></li><li class="h3 ng-star-inserted" title="不要给输出属性加前缀"><a href="guide/styleguide#dont-prefix-output-properties">不要给输出属性加前缀</a></li><li class="h3 ng-star-inserted" title="把表现层逻辑放到组件类里"><a href="guide/styleguide#put-presentation-logic-in-the-component-class">把表现层逻辑放到组件类里</a></li><li class="h2 ng-star-inserted" title="指令"><a href="guide/styleguide#directives">指令</a></li><li class="h3 ng-star-inserted" title="使用指令来增强已有元素"><a href="guide/styleguide#use-directives-to-enhance-an-element">使用指令来增强已有元素</a></li><li class="h3 ng-star-inserted" title="HostListener 和 HostBinding 装饰器 vs. 组件元数据 host"><a href="guide/styleguide#hostlistenerhostbinding-decorators-versus-host-metadata"><em>HostListener</em> 和 <em>HostBinding</em> 装饰器 vs. 组件元数据 <em>host</em></a></li><li class="h2 ng-star-inserted" title="服务"><a href="guide/styleguide#services">服务</a></li><li class="h3 ng-star-inserted" title="服务总是单例的"><a href="guide/styleguide#services-are-singletons">服务总是单例的</a></li><li class="h3 ng-star-inserted" title="单一职责"><a href="guide/styleguide#single-responsibility-1">单一职责</a></li><li class="h3 ng-star-inserted" title="提供一个服务"><a href="guide/styleguide#providing-a-service">提供一个服务</a></li><li class="h3 ng-star-inserted" title="使用 @Injectable() 类装饰器"><a href="guide/styleguide#use-the-injectable-class-decorator">使用 @Injectable() 类装饰器</a></li><li class="h2 ng-star-inserted" title="数据服务"><a href="guide/styleguide#data-services">数据服务</a></li><li class="h3 ng-star-inserted" title="通过服务与 Web 服务器通讯"><a href="guide/styleguide#talk-to-the-server-through-a-service">通过服务与 Web 服务器通讯</a></li><li class="h2 ng-star-inserted" title="生命周期钩子"><a href="guide/styleguide#lifecycle-hooks">生命周期钩子</a></li><li class="h3 ng-star-inserted" title="实现生命周期钩子接口"><a href="guide/styleguide#implement-lifecycle-hook-interfaces">实现生命周期钩子接口</a></li><li class="h2 ng-star-inserted" title="附录"><a href="guide/styleguide#appendix">附录</a></li><li class="h3 ng-star-inserted" title="Codelyzer"><a href="guide/styleguide#codelyzer">Codelyzer</a></li><li class="h3 ng-star-inserted" title="文档模板和代码片段"><a href="guide/styleguide#file-templates-and-snippets">文档模板和代码片段</a></li></ul></div></aio-toc></aio-lazy-ce></div><footer class="no-print"><aio-footer><div class="grid-fluid"><div class="footer-block ng-star-inserted"><h3>资源</h3><ul><li class="ng-star-inserted"><a class="link" href="about" title="Angular 贡献者。">关于</a></li><li class="ng-star-inserted"><a class="link" href="resources" title="网络上的 Angular 工具、培训、博客等">资源列表</a></li><li class="ng-star-inserted"><a class="link" href="presskit" title="我们的联系方式、LOGO 和品牌">宣传资料</a></li><li class="ng-star-inserted"><a class="link" href="https://blog.angular.io/" title="Angular 官方博客">博客</a></li><li class="ng-star-inserted"><a class="link" href="analytics" title="Angular 使用情况分析">使用情况分析</a></li></ul></div><div class="footer-block ng-star-inserted"><h3>帮助</h3><ul><li class="ng-star-inserted"><a class="link" href="https://stackoverflow.com/questions/tagged/angular" title="Stack Overflow: 这里的社区会回答你关于 Angular 的技术问题">Stack Overflow</a></li><li class="ng-star-inserted"><a class="link" href="https://gitter.im/angular/angular" title="和老鸟聊 Angular">Gitter</a></li><li class="ng-star-inserted"><a class="link" href="https://github.com/angular/angular/issues" title="在 github 上报告问题和建议。">报告问题</a></li><li class="ng-star-inserted"><a class="link" href="https://github.com/angular/code-of-conduct/blob/master/CODE_OF_CONDUCT.md" title="让我们彼此尊重">行为规范</a></li></ul></div><div class="footer-block ng-star-inserted"><h3>社区</h3><ul><li class="ng-star-inserted"><a class="link" href="events" title="Angular events around the world.">活动</a></li><li class="ng-star-inserted"><a class="link" href="http://www.meetup.com/topics/angularjs/" title="参加聚会，向别的开发人员学习">聚会</a></li><li class="ng-star-inserted"><a class="link" href="https://twitter.com/angular" title="Twitter">Twitter</a></li><li class="ng-star-inserted"><a class="link" href="https://github.com/angular/angular" title="GitHub">GitHub</a></li><li class="ng-star-inserted"><a class="link" href="contribute" title="向 Angular 做贡献">做贡献</a></li></ul></div><div class="footer-block ng-star-inserted"><h3>多语言</h3><ul><li class="ng-star-inserted"><a class="link" href="https://angular.io/" title="English Version.">English Version</a></li><li class="ng-star-inserted"><a class="link" href="https://angular.tw/" title="正體中文版">正體中文版</a></li><li class="ng-star-inserted"><a class="link" href="https://angular.jp/" title="日本語版">日本語版</a></li><li class="ng-star-inserted"><a class="link" href="https://angular.kr/" title="한국어">한국어</a></li></ul></div></div><p>Super-powered by Google ©2010-2020. 代码授权方式：<a href="license" title="License text">MIT-style License</a>. 文档授权方式：<a href="http://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a>.</p><p>当前版本：9.0.1-build.79+sha.55f1ef01.</p></aio-footer></footer><div class="cdk-visually-hidden ng-star-inserted"><mat-icon role="img" class="mat-icon notranslate material-icons mat-icon-no-color" aria-hidden="true">&nbsp;</mat-icon></div></aio-shell><noscript><div class="background-sky hero"></div><section id="intro" style="text-shadow:1px 1px #1976d2"><div class="hero-logo"><img src="assets/images/logos/angular/angular.svg" width="250" height="250" alt="Angular"></div><div class="homepage-container"><div class="hero-headline">一套框架，多种平台<br>移动 &amp; 桌面</div></div></section><h2 style="color:red;margin-top:40px;position:relative;text-align:center;text-shadow:1px 1px #fafafa"><b><i>该网站需要浏览器支持 JavaScript</i></b></h2></noscript><script src="runtime-es2015.ee607cf78010348c8334.js" type="module"></script><script src="runtime-es5.ee607cf78010348c8334.js" nomodule="" defer=""></script><script src="polyfills-es5.1494ca908613f3429457.js" nomodule="" defer=""></script><script src="polyfills-es2015.9057a05c650bf60ecbf4.js" type="module"></script><script src="main-es2015.9cd62e243bd182514a19.js" type="module"></script><script src="main-es5.9cd62e243bd182514a19.js" nomodule="" defer=""></script><div class="cdk-live-announcer-element cdk-visually-hidden" aria-atomic="true" aria-live="polite"></div></body></html>